Skip to main content

You can transform XML data by applying an XSLT transformation.

Example: XSLT transformation

As an example, you can transform this input document:

<Accredited_Persons documentName="Accredited Persons">
<PERSON ID="177" FAMILY_NAME="Haynes" FIRST_NAME="Deborah"/>
<PERSON ID="112" FAMILY_NAME="Smith" FIRST_NAME="John"/>
</Accredited_Persons>

to this different format:

<Persons">
<Person ID="177">
<Name>Deborah Haynes</Name>
</Person>
<Person ID="112">
<Name>John Smith</Name>
</Person>
</Persons>

by applying this XSLT transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="Accredited_Persons">
<Persons>
<xsl:apply-templates select="*|text()|comment()|processing-instruction()"/>
</Persons>
</xsl:template>
<xsl:template match="PERSON">
<Person>
<Name>
<xsl:value-of select="concat(@FIRST_NAME,' ',@FAMILY_NAME)"/>
</Name>
</Person>
</xsl:template>
<!-- Default identity transformation: Copy any remaining matched content from input to output> -->
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()|comment()|processing-instruction()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

You can achieve this in a USoft context by calling methods of the internal USXSL component.

Applying a transformation in a USoft context

In a USoft context, call methods of the USXSL internal component to apply an XSLT transformation. If the input document, the transformation and the output document are all on the file system, use a statement like this:

SELECT    USXSL.Apply2File(
'C:\MyInput.xml'
, 'C:\MyTransformation.xsl'
, 'C:\MyOutput.xml'
)

or:

INVOKE    USXSL.Apply2File WITH
SELECT 'C:\MyInput.xml'
, 'C:\MyTransformation.xsl'
, 'C:\MyOutput.xml'

If the input document and the transformation are stored in a USoft application table (in columns based on CLOB or NCLOB domains), use a statement like this:

SELECT    USXSL.Apply2File(
p.xml_document
, t.transformation_xsl
, 'C:\MyOutput.xml'
FROM person p
, transformation t
where t.name = 'transform_persons'
)

You can get data from an external source and transform it to a data format that USoft can import. You can use a statement like this. This example also illustrates that the XSLT transformation may be passed as a literal:

INVOKE xml.import WITH
(
SELECT usxsl.apply
(
'c:\temp\Tour_Programmes.xml'
, '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()|comment()|processing-instruction()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>'
)
)

 

Be the first to reply!

Reply