XSLT Include: Merging and Reusing Transformations 🎯

beginner
15 min

XSLT Include: Merging and Reusing Transformations 🎯

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.

What is XSLT Include? 📝

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.

Why Use XSLT Include? 💡

  1. Code Reusability: By breaking down a large stylesheet into smaller, reusable modules, we can reduce redundancy and make our code more efficient.
  2. Maintenance: Modularization makes it easier to update and maintain our XSLT stylesheets, as we can modify individual modules without affecting the entire stylesheet.
  3. Improved Organization: XSLT Include allows us to organize our stylesheets logically, making it easier to understand and navigate.

Basic Syntax 📝

An XSLT Include statement looks like this:

xml
<xsl:include href="uri"/>

Here, href is the URI (Uniform Resource Identifier) of the external file that we want to include.

Including Stylesheets 💡

Let's take an example to understand how to include an XSLT stylesheet.

Create a file named style1.xsl with the following content:

xml
<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:

xml
<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:

xml
<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:

xml
<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
<html> <body> <h1>This is Style 1</h1> <p>1: Book 1</p> <p>2: Book 2</p> </body> </html>

Including XML Documents 💡

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:

xml
<root> <child1>Content 1</child1> <child2>Content 2</child2> </root>

Now, create an XSLT file named main.xsl with the following content:

xml
<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
<html> <body> <h1>Included XML Document</h1> <p>Content 1</p> <p>Content 2</p> </body> </html>

Summary ✅

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.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the purpose of XSLT Include?