Including XSLT documents


Importing XSLT documents gives only a specific amount of control. It is possible to get more control of which xsl:template is viewed and how it is viewed by using the xsl:include instruction. To continue the example from the preceding section, consider the following XSLT, which uses the xsl:include instruction:

<xsl:stylesheet version=”1.0”  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>     <xsl:include href=”included.xsl”/>     <xsl:template match=”elements” mode=”embedded”>         <xsl:apply-templates mode=”embedded”/>          Mode Embedded (<xsl:value-of select=”text()” />)     </xsl:template>     <xsl:template match=”elements”>         <xsl:apply-templates mode=”embedded”/>          Top level (<xsl:value-of select=”text()” />)     </xsl:template>     <xsl:template match=”text()” mode=”?”>     </xsl:template> </xsl:stylesheet>

Executing this XSLT results in the following output:

<?xml version=”1.0” encoding=”UTF-8”?>          Top level (Hello)               Mode Embedded (Embedded)               Top level ()

This output is no surprise because it is identical to the xsl:import instruction. In this simple example, xsl:include does indeed act like xsl:import. Consider the following XSLT document that has moved the xsl:include instruction to a farther-removed location in the XSLT document:

<xsl:stylesheet version=”1.0”  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>     <xsl:template match=”elements” mode=”embedded”>         <xsl:apply-templates mode=”embedded”/>          Mode Embedded (<xsl:value-of select=”text()” />)     </xsl:template>     <xsl:include href=”included.xsl”/>      <xsl:template match=”elements”>         <xsl:apply-templates mode=”embedded”/>          Top level (<xsl:value-of select=”text()” />)     </xsl:template>     <xsl:template match=”text()” mode=”?”>     </xsl:template> </xsl:stylesheet>

Executing this XSLT results in the following output:

<?xml version=”1.0” encoding=”UTF-8”?>          Top level (Hello)               Included (Embedded)               Top level ()

In this output, the included xsl:template that has the same match overrides the xsl:template instruction defined locally. By moving the xsl:include instruction, it shows that the xsl:include instruction can be a top-level child element anywhere in the document. It shows that depending on the location of the xsl:include statement, specific output can be generated.

Another way of controlling which xsl:template is matched by the XSLT processor when evaluating the XML document nodes is to use the priority attribute on the xsl:template instruction. Consider the following modification to the included XSLT document:

<xsl:template match=”elements” mode=”embedded” priority=”2”>     <xsl:apply-templates mode=”embedded”/>      Included (<xsl:value-of select=”text()” />) </xsl:template>

The xsl:template instruction has the added attribute priority that is set to a value of 2. Now consider the modified XSLT document that includes the previous XSLT document:

<xsl:template match=”elements” mode=”embedded” priority=”3”>     <xsl:apply-templates mode=”embedded”/>      Mode Embedded (<xsl:value-of select=”text()” />) </xsl:template>

In this example, the priority is set to 3, which is one higher than the included priority value. Executing the XSLT documents results in the following output:

<?xml version=”1.0” encoding=”UTF-8”?>          Top level (Hello)               Mode Embedded (Embedded)               Top level ()

The output has changed from the previous output, with only the change in the priority of the template. A higher priority means higher precedence. Because the locally defined template priority is higher than the included defined template, the locally defined template is matched.

Priorities are useful because they allow you to include various templates and to make sure a specific template is called, regardless of how it is included. If a template does not have priority and another does, the template without a priority is considered to have a priority value of zero.




The XMLSPY Handbook
The Official XMLSPY Handbook
ISBN: 764549642
EAN: 2147483647
Year: 2001
Pages: 121
Authors: Larry Kim

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net