Welcome to our comprehensive guide on XSLT Include! In this lesson, we'll dive deep into understanding how to merge and reuse transformations using XSLT Include. By the end of this tutorial, you'll have a solid grasp of this powerful feature that will help you write more efficient and maintainable XSLT stylesheets.
XSLT Include is an XML namespace feature that allows us to modularize our XSLT stylesheets. It enables us to split a large stylesheet into smaller, reusable pieces, making our code more manageable and easier to maintain.
An XSLT Include statement looks like this:
<xsl:include href="uri"/>Here, href is the URI (Uniform Resource Identifier) of the external file that we want to include.
Let's take an example to understand how to include an XSLT stylesheet.
Create a file named style1.xsl with the following content:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>This is Style 1</h1>
<xsl:apply-templates select="/"/>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<p><xsl:value-of select="title"/></p>
</xsl:template>
</xsl:stylesheet>Now, create another file named style2.xsl with the following content:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://www.codeyourcraft.com/ns">
<xsl:import namespace="http://www.codeyourcraft.com/ns"/>
<xsl:template match="my:book">
<p><xsl:value-of select="@id"/>: <xsl:value-of select="title"/></p>
</xsl:template>
</xsl:stylesheet>Lastly, create an XML file named books.xml with the following content:
<books xmlns:my="http://www.codeyourcraft.com/ns">
<book id="1" title="Book 1"></book>
<book id="2" title="Book 2"></book>
</books>To apply both style1.xsl and style2.xsl to books.xml, we can create a third XSLT file named main.xsl with the following content:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://www.codeyourcraft.com/ns">
<xsl:include href="style1.xsl"/>
<xsl:include href="style2.xsl"/>
</xsl:stylesheet>When you apply main.xsl to books.xml, you'll get the following output:
<html>
<body>
<h1>This is Style 1</h1>
<p>1: Book 1</p>
<p>2: Book 2</p>
</body>
</html>XSLT Include can also be used to include XML documents. To do this, we can use the <xsl:copy-of select="document('uri')"/> instruction.
Let's create an XML file named example.xml with the following content:
<root>
<child1>Content 1</child1>
<child2>Content 2</child2>
</root>Now, create an XSLT file named main.xsl with the following content:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="example.xml"/>
<xsl:template match="root">
<html>
<body>
<h1>Included XML Document</h1>
<xsl:apply-templates select="child1"/>
<xsl:apply-templates select="child2"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>When you apply main.xsl, the included XML document example.xml is processed, and the output is:
<html>
<body>
<h1>Included XML Document</h1>
<p>Content 1</p>
<p>Content 2</p>
</body>
</html>In this tutorial, we learned about XSLT Include, a powerful feature that allows us to merge and reuse transformations in our XSLT stylesheets. We covered the basic syntax, demonstrated how to include XSLT stylesheets and XML documents, and saw practical examples of their usage.
What is the purpose of XSLT Include?