Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Reference

Example 1 of <xsl:apply-imports>

This example demonstrates how to use <xsl:apply-imports> to reuse code more effectively. The example uses four main files:

Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (ops.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ops.xsl"?>
<ops>
  <desc>Some binary operations</desc>
  <op name="add" symbol="+">
    <operand>1</operand>
    <operand>2</operand>
  </op>
  <op name="sub" symbol="-">
    <operand>1</operand>
    <operand>2</operand>
  </op>
  <op name="mul" symbol="*">
    <operand>1</operand>
    <operand>2</operand>
  </op>
</ops>

Main XSLT File (ops.xsl)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0">
  <xsl:import href="arith.xsl"/>
  <xsl:import href="str.xsl"/>
  <xsl:template match="op">
    <xsl:value-of select="operand[1]"/>
    <xsl:value-of select="@symbol"/>
    <xsl:value-of select="operand[2]"/>
    = <xsl:apply-imports/>
    <br/>
  </xsl:template>
</xsl:stylesheet>

Imported XSLT File (arith.xsl)

<?xml version="1.0"?>

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

version="1.0">

<xsl:template match="op[@symbol='+']">

<xsl:value-of select="sum(operand)"/> (from arith.xsl)

</xsl:template>

<xsl:template match="op[@symbol='-']">

<xsl:value-of select="number(operand[1])-number(operand[2])"/>

(from arith.xsl)

</xsl:template>

<xsl:template match="op[@symbol='*']">

<xsl:value-of select="number(operand[1])*number(operand[2])"/>

(from arith.xsl)

</xsl:template>

</xsl:stylesheet>

Imported XSLT File (str.xsl)

<?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                  version="1.0">
  <xsl:template match="desc">
    <DIV><xsl:value-of select="."/></DIV>
  </xsl:template>
  <xsl:template match="op[@name='add']">
    <xsl:value-of select="operand[1]"/>
    <xsl:value-of select="operand[2]"/> (from str.xsl)
  </xsl:template>
  <xsl:template match="op[@name='mul']">
    <xsl:value-of select="operand[2]"/>
    <xsl:value-of select="operand[1]"/> (from str.xsl)
  </xsl:template>
</xsl:stylesheet>

Output

You should get the following output:

Some binary operations
1+2 = 12 (from str.xsl) 
1-2 = -1 (from arith.xsl) 
1*2 = 21 (from str.xsl)

Remarks

The last imported style sheet has the highest import precedence. In this example, str.xsl is imported last, and therefore has a higher import precedence than arith.xsl. Both imported style sheets have templates for add and mul operations. Only those from str.xsl are called. The sub operation defined in arith.xsl is used, however, because no sub operation is defined in str.xsl.

Suppose we reversed the order of the <xsl:import> elements in the main XSLT file, like this:  <xsl:import href="str.xsl"/>
  <xsl:import href="arith.xsl"/>

In this case, the output would be the following:

Some binary operations
1+2 = 3 (from arith.xsl) 
1-2 = -1 (from arith.xsl) 
1*2 = 2 (from arith.xsl)

Also, if the <xsl:apply-imports/> instruction is absent from the overriding template rule for the <op> in the main XSLT file (ops.xsl), the output is the following:

Some binary operations
1+2 = 
1-2 = 
1*2 = 

That is, the template rule in the importing style sheet overrides the related template rules in the imported style sheets. The <xsl:apply-imports/> instruction lets you reactivate these overridden template rules in a different manner.