共用方式為


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:

  • The XML source file, ops.xml. This data file defines three operations: add (+), sub (-) and mul (*).

  • The main XSLT style sheet, ops.xsl. This file contains the template rules for the operations, including two <xsl:import> elements. The imported style sheets perform the arithmetic and string operations on a given data source.

  • An imported style sheet, arith.xsl. This XSLT file performs arithmetic operations on each <op> element.

  • Another imported style sheet, str.xsl. This XSLT file performs custom string operations. Here add (+) is treated as a string concatenation; for example, 1+2 becomes 12. Similarly, mul (*) is treated as a reverse concatenation; 1*2 becomes 21. Notice that sub (-) is an undefined string operation.

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.