xsl:import

  1. import_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.   <product id="p4" name="Foxtrot" price="1500" stock="5"/>
  8.   <product id="p5" name="Tango" price="1225" stock="3"/>
  9. </products>
  1. import_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="import_extra.xsl"/>
  5.   <xsl:output indent="yes"/>
  6.   <xsl:template match="/">
  7.     <PRODUCTS>
  8.       <xsl:apply-templates/>
  9.     </PRODUCTS>
  10.   </xsl:template>
  11. </xsl:stylesheet>
  1. import_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. import_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <PRODUCTS>
  4.   <PRODUCT id="p1" price="3250" stock="4"/>
  5.   <PRODUCT id="p2" price="1000" stock="5"/>
  6.   <PRODUCT id="p3" price="1200" stock="19"/>
  7.   <PRODUCT id="p4" price="1500" stock="5"/>
  8.   <PRODUCT id="p5" price="1225" stock="3"/>
  9. </PRODUCTS>

Comment

xsl:import works exactly like xsl:include except that xsl:import must be the first child under xsl:stylesheet. For that reason templates in the importing stylesheet will always come last and get their way, if we have conflicts between templates matching with same priority.

Updated 2009-03-19