XSLT 1.0 Lookup Function with Embedded XML and Keys
XSLT 1.0 provides a powerful way to transform XML data, but sometimes you need to look up information within your stylesheet itself. This is where the document('')
function and key
declarations come in handy. This article will guide you through creating a lookup function using embedded XML and keys in XSLT 1.0.
Scenario: Imagine you have an XML file containing product information, and you need to look up the product category based on the product ID in your XSLT stylesheet.
Original Code Example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="productCategory" match="product" use="@id"/>
<xsl:template match="/">
<xsl:apply-templates select="products/product"/>
</xsl:template>
<xsl:template match="product">
<product>
<id><xsl:value-of select="@id"/></id>
<name><xsl:value-of select="name"/></name>
<category><xsl:value-of select="key('productCategory', @id, document('product-data.xml'))/category"/></category>
</product>
</xsl:template>
</xsl:stylesheet>
Explanation:
- Key Declaration: The
xsl:key
declaration defines a key named "productCategory" that matches elements named "product" and uses their "@id" attribute as the key value. document('')
Function: Thedocument('')
function reads the content of theproduct-data.xml
file into the XSLT stylesheet.key()
Function: Thekey()
function retrieves the matching "product" element based on the provided key value (@id).- Lookup and Output: The
category
element is then retrieved from the matched product element within the embedded XML.
Analysis and Clarification:
- Internal Lookup: This approach allows you to perform a lookup directly within the XSLT stylesheet, eliminating the need for external lookups.
- Key Declarations: Keys are essential for efficient data retrieval. They enable the XSLT processor to quickly identify the desired element.
- Embedded XML: The
document('')
function reads the XML content into the XSLT stylesheet. This approach can be helpful for small datasets that are regularly used.
Example:
Let's assume the following XML structure for the product-data.xml
file:
<products>
<product id="1">
<name>Laptop</name>
<category>Electronics</category>
</product>
<product id="2">
<name>Shirt</name>
<category>Clothing</category>
</product>
</products>
The XSLT code will output the following:
<product>
<id>1</id>
<name>Laptop</name>
<category>Electronics</category>
</product>
<product>
<id>2</id>
<name>Shirt</name>
<category>Clothing</category>
</product>
Additional Value:
- Maintainability: Having data embedded within the XSLT stylesheet can improve maintainability, especially when dealing with small, static datasets.
- Flexibility: You can easily modify the embedded XML data without changing the XSLT logic.
- Simplified Processing: This technique eliminates the need for external lookup calls, potentially improving performance.
References:
Conclusion:
By utilizing the document('')
function and key declarations in XSLT 1.0, you can efficiently create lookup functions that directly access embedded XML data. This approach offers several benefits, including simplified processing, improved maintainability, and flexibility in data management.