Welcome to the XSLT Import lesson! In this tutorial, we'll learn how to import and transform XML documents using XSLT (eXtensible Stylesheet Language Transformations). By the end of this lesson, you'll have a solid understanding of XSLT imports and be able to apply them to your own projects. 📝 Note: XSLT is an essential tool for manipulating and converting XML data into different formats such as HTML, JSON, or other XML documents.
<a name="what-is-xslt-import"></a>
XSLT Import is a feature that allows you to combine multiple XML files and transform them using a single XSLT stylesheet. This is particularly useful when dealing with large or complex XML documents. By importing other XML files, you can reuse existing data and reduce the amount of code you need to write.
<a name="why-use-xslt-import"></a>
Using XSLT Import offers several advantages:
<a name="xslt-import-syntax"></a>
The XSLT Import syntax uses the xsl:import element to include other XSLT stylesheets. Here's the basic syntax:
<xsl:import href="url" />Replace url with the location of the external XSLT stylesheet you want to import.
<a name="step-by-step-example"></a>
Let's walk through an example to illustrate how to import and transform XML data using XSLT.
products.xml)<products>
<product id="1">
<name>Product 1</name>
<price>19.99</price>
</product>
<product id="2">
<name>Product 2</name>
<price>29.99</price>
</product>
<!-- More products... -->
</products>products.xsl)In this example, we'll create an XSLT stylesheet that lists the product names and prices. We'll also import another XSLT stylesheet to handle the table formatting.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Import the table formatting stylesheet -->
<xsl:import href="table-format.xsl" />
<!-- Define the template for the products element -->
<xsl:template match="/products">
<xsl:apply-templates select="product" />
</xsl:template>
<!-- Define the template for the product element -->
<xsl:template match="product">
<xsl:variable name="product-id" select="@id" />
<xsl:call-template name="product-table">
<xsl:with-param name="id" select="$product-id" />
<xsl:with-param name="name" select="name" />
<xsl:with-param name="price" select="price" />
</xsl:call-template>
</xsl:template>
<!-- Include the table formatting template from the imported stylesheet -->
<xsl:include href="table-format.xsl#product-table" />
</xsl:stylesheet>table-format.xsl)This example includes a simple table formatting stylesheet that we'll import in the main XSLT stylesheet.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Define the template for the product-table function -->
<xsl:template name="product-table">
<table border="1">
<tr>
<th>{$id}</th>
<th>Name</th>
<th>Price</th>
</tr>
<tr>
<td>{$id}</td>
<td><xsl:value-of select="$name" /></td>
<td><xsl:value-of select="$price" /></td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>To apply the XSLT transformation, save both XML files (products.xml and products.xsl) in the same directory and use an XSLT processor like Saxon, libxslt, or Microsoft XSLT. The output will be an HTML document displaying the product data in a table format.
<a name="xslt-import-best-practices"></a>
<a name="quiz"></a>
What is the purpose of the `xsl:import` element in XSLT?
That's it for this XSLT Import tutorial! By learning how to import and transform XML data using XSLT, you'll be well-equipped to handle a variety of XML manipulation tasks in your projects. 💡 Pro Tip: Practice using XSLT imports with different XML documents and XSLT stylesheets to solidify your understanding of this powerful tool. ✅ Happy coding!