Skip to content

XSLT <xsl:import>

How to use the <xsl:import> element in XSLT to merge stylesheets while maintaining proper precedence rules for template matching.

XSLT <xsl:import>

The <xsl:import> element is a top-level element that is used to import the contents of one style sheet into another.

Syntax

<xsl:import href="URI"/>

Attributes

Attribute Value Required/Optional Description
href URI Required Specifies the URI of the style sheet to import

Example

Suppose you have a style sheet called "bookstore_style.xsl":

bookstore_style.xslxml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>The bookstore</h2>
    <table border="1">
      <tr bgcolor="#6565d5">
        <th>Title</th>
        <th>Author</th>
      </tr>
      <tr>
        <td><xsl:value-of select="bookstore/book/title"/></td>
        <td><xsl:value-of select="bookstore/book/author"/></td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

And a second style sheet called "bookstore_import.xsl" imports "bookstore_style.xsl":

bookstore_import.xslxml
<?xml version="1.0"  encoding="UTF-8"?>  
<xsl:stylesheet version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  
<xsl:import href="cdcatalog_ex3.xsl"/>  
  
<xsl:template match="/">  
	<xsl:apply-imports/>  
</xsl:template>  
  
</xsl:stylesheet>
Navigation