Importing XSLT documents


XSLT would not be a complete programming language if it did not include a mechanism to include other XSLT documents. This makes it possible to modularize your XSLT documents. There are two different instructions needed to include another XSLT document, xsl:include and xsl:import. In either instruction, there exists an XSLT document that will be inserted into the parent document. In the simplest case, consider the following already-existing XSLT document:

<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/ XSL/Transform” xmlns:fo=”http://www.w3.org/1999/XSL/Format”>     <xsl:template match=”elements” mode=”embedded”>         <xsl:apply-templates mode=”embedded”/>          Included (<xsl:value-of select=”text()” />)     </xsl:template> </xsl:stylesheet>

This document is called included.xsl and can be imported as shown in the following example:

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

This example is a modification of the mode example, where the mode-based xsl:template has been moved into its own XSLT file. The xsl:import instruction is a top-level instruction. A top-level instruction is an instruction that must be a direct child of the xsl:stylesheet instruction. In the case of the xsl:import instruction, it must also be directly after the xsl:stylesheet instruction. The xsl:import instruction has the href attribute, which specifies the location of the file to import. The value is a URI, which means the reference could be downloaded from a server. Executing the XSLT document will generate the following output:

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

If the XSLT document doing the importing has a locally defined template match, that template match takes precedence over the one defined within the imported XSLT document. Importing documents doesn’t give you much control in overriding specific templates. However, this method of overriding templates is useful when you want to ensure that local implementations are executed before imported implementations.

If the total override of a template match is not desirable, it is possible to call both the imported version of the template match and the local version. An example of this is in the following modified XSLT document doing the importing:

<xsl:stylesheet version=”1.0”  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>     <xsl:import 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-imports />         <xsl:apply-templates mode=”embedded”/>          (<xsl:value-of select=”text()” />)     </xsl:template>     <xsl:template match=”text()” mode=”?”>     </xsl:template> </xsl:stylesheet>

In the modified XSLT document, the original xsl:template instruction with a match of elements in mode embedded is added to the XSLT document. Now, with both matches present to be able to execute both templates, an xsl:apply-modes instruction is added. This instruction is similar in action to the xsl:apply-templates instruction, except that it applies to all the xsl:template instructions. Executing this modified XSLT document will result in the following output:

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

The downside to using this functionality is that all matching xsl:templates are called. Therefore, the objective of using this functionality is to extend existing template actions. Also be aware that in the example XSLT, the xsl:apply-imports instruction was before the xsl:apply-templates instruction. If the positions are reversed, the output is slightly modified as well.




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