xsl:apply-imports

  1. apply-imports_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" name="Delta" price="3250" stock="4"/>
  5.   <product id="p2" name="Golf" price="1000" stock="5"/>
  6.   <product id="p3" name="Alpha" price="1200" stock="19"/>
  7. </products>
  1. apply-imports_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:import href="apply-imports_extra.xsl"/>
  5.   <xsl:output indent="yes"/>
  6.   <xsl:template match="/">
  7.     <PRODUCTS>
  8.       <xsl:apply-templates/>
  9.     </PRODUCTS>
  10.   </xsl:template>
  11.   <xsl:template match="product">
  12.     <xsl:comment>
  13.       <xsl:value-of select="concat(' ', @name, ' ')"/>
  14.     </xsl:comment>
  15.     <xsl:text>&#xA;</xsl:text>
  16.     <xsl:apply-imports/>
  17.   </xsl:template>
  18. </xsl:stylesheet>
  1. apply-imports_1-stylesheet-extra.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:template match="product">
  5.     <PRODUCT id="{@id}" price="{@price}" stock="{@stock}"/>
  6.   </xsl:template>
  7. </xsl:stylesheet>
  1. apply-imports_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <PRODUCTS>
  4.   <!-- Delta -->
  5.   <PRODUCT id="p1" price="3250" stock="4"/>
  6.   <!-- Golf -->
  7.   <PRODUCT id="p2" price="1000" stock="5"/>
  8.   <!-- Alpha -->
  9.   <PRODUCT id="p3" price="1200" stock="19"/>
  10. </PRODUCTS>

Comment

xsl:apply-imports makes is possible to have two templates matching the same thing without over-ruling one another. Linefeed = &#xA;

Updated 2009-03-19