Chapter 24 Performs the XSLT transformation

Perform XSLT transformations

To perform the XSLT transformation, do the following:

  • Student: If you use thetaXalanProcessor (forXSLT 1.0), please use%XML.XSLT.TransformerOne of the following class methods:
    • TransformFile()Transform the file for the given XSLT stylesheet.
    • TransformFileWithCompiledXSL()Transform a file, given a compiled XSLT stylesheet.
    • TransformStream()Transform the flow of the given XSLT stylesheet.
    • TransformStreamWithCompiledXSL()Transform a flow, given a compiled XSLT stylesheet.
    • TransformStringWithCompiledXSL()— Transforms the string given the compiled XSLT stylesheet.
  • If you are usingSaxonProcessor (for XSLT 2.0), use%XML.XSLT2.TransformerOne of the following class methods:
    • TransformFile()Transform the file for the given XSLT stylesheet.
    • TransformFileWithCompiledXSL()Transform a file, given a compiled XSLT stylesheet.
    • TransformStream()Transform the flow of the given XSLT stylesheet.
    • TransformStreamWithCompiledXSL()Transform a flow, given a compiled XSLT stylesheet.

These methods have similar signatures. The argument list for these methods is as follows:

  • PSource – The source XML to be transformed. See the table following this list.
  • Pxsl-style sheet or compile style sheet. See the table following this list.
  • POutput – The result XML returned as an output parameter. See the table following this list.
  • PErrorHandler – An optional custom error handler. See “Custom Error Handling” later in this chapter. If you do not specify a custom ErrorHandler, the method uses a new instance of % xml.xslt.ErrorHandler (for both classes).
  • PParms – An optional InterSystems IRIS multidimensional array containing parameters to be passed to the stylesheet.
  • PCallbackHandler definition –XSLTThe optional callback handler for the extension function.
  • PResolver – An optional entity resolver.
  1. (Applicable only%XML.XSLT2.Transformer) the gateway –%Net.Remote.GatewayOptional instance of. Specify this parameter if you want to reuse XSLT gateway connections for better performance;

For reference, the table below shows the first three parameters of these methods and compares them:

Comparison of XSLT transformation methods

Method pSource (Input XML) pXSL (Stylesheet) pOutput(Output XML)
TransformFile() String that gives a file name String that gives a file name String that gives a file name
TransformFileWithCompiledXSL() String that gives a file name Compiled style sheet String that gives a file name
TransformStream() Stream Stream Stream, returned by reference
TransformStreamWithCompiledXSL() Stream Compiled style sheet Stream, returned by reference
TransformStringWithCompiledXSL() String Compiled style sheet String that gives a file name

The sample

This section shows several transformations using the following code (but different input files) :

  Set in="c:\0test\xslt-example-input.xml"
  Set xsl="c:\0test\xslt-example-stylesheet.xsl"
  Set out="c:\0test\xslt-example-output.xml"
  Set tSC=##class(%XML.XSLT.Transformer).TransformFile(in.xsl.out)
  Write tSC 
Copy the code

Example 1: Simple substitution

In this case, we start with the following input XML:


      
<s1 title="s1 title attr">
  <s2 title="s2 title attr">
    <s3 title="s3 title attr">Content</s3>
  </s2>
</s1>
Copy the code

We use the following stylesheets:


      

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>
 
<xsl:template match="//@* | //node()">
  <xsl:copy>
    <xsl:apply-templates select="@ *"/>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="/s1/s2/s3">
<xsl:apply-templates select="@ *"/>
<xsl:copy>
Content Replaced
</xsl:copy>
</xsl:template>  

</xsl:stylesheet>
Copy the code

In this case, the output file would look like this:


      
<s1 title="s1 title attr">
  <s2 title="s2 title attr">
    <s3>
Content Replaced
</s3>
  </s2>
</s1>
Copy the code

Example 2: Content extraction

In this case, we start with the following input XML:


      
<MyRoot>
   <MyElement No="13">Some text</MyElement>
   <MyElement No="14">Some more text</MyElement>
</MyRoot>
Copy the code

We use the following stylesheets:


      
<xsl:stylesheet version="1.1"   
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"
            media-type="text/plain"/>
<xsl:strip-space elements="*"/>

<! -- utilities not associated with specific tags -->
<! -- emit a newline -->
<xsl:template name="NL">
    <xsl:text>&#xa;</xsl:text>
</xsl:template>

<! -- beginning of processing -->

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="MyElement">
    <xsl:value-of select="@No"/>
    <xsl:text>: </xsl:text>
    <xsl:value-of select="."/>
    <xsl:call-template name="NL"/>
</xsl:template>

</xsl:stylesheet>
Copy the code

In this case, the output file would look like this:

13: Some text
14: Some more text
Copy the code

Other examples

InterSystems IRIS provides the following additional examples:

  • forXSLT 1.0, please refer to the%XML.XSLT.TransformerIn theExample(),Example2()And other methods.
  • forXSLT 2.0Please see theSamplesClasses in a namespaceXSLT2.Examples.