Understanding XSLT 2.0 xsl:key and key() Function for Efficient Node Lookup

In XSLT, the id() function can be used to locate nodes, but it has certain limitations. For instance, ID-type values cannot be pure numbers, and their values must conform to XML naming rules.

This limitation can be inconvenient in specific scenarios, such as locating books by ISBN numbers, wich are pure numeric identifiers and don't fit the ID type requirements.

Let's explore the application of xsl:key and key() functions in XSLT using a product catalog example.

XSLT Processor: Saxon 9

Command: java net.sf.saxon.Transform -xsl:products.xslt -s:products.xml -o:products.html

XML Data Source: products.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="products.xslt" type="text/xsl"?>
<!-- Product catalog information -->
<catalog>
    <product>
        <sku>PRD-001</sku>
        <name>Wireless Mouse</name>
        <price>29.99USD</price>
        <category>Electronics</category>
    </product>
    <product>
        <sku>PRD-002</sku>
        <name>Bluetooth Speaker</name>
        <price>79.99USD</price>
        <category>Electronics</category>
    </product>
    <product>
        <sku>PRD-003</sku>
        <name>Office Chair</name>
        <price>149.99USD</price>
        <category>Furniture</category>
    </product>
    <product>
        <sku>PRD-004</sku>
        <name>Desk Lamp</name>
        <price>39.99USD</price>
        <category>Furniture</category>
    </product>
</catalog>

XSLT File: products.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:preserve-space elements="catalog"/>
    <xsl:key name="sku" match="product" use="normalize-space(sku)"/>
    <xsl:key name="category" match="product" use="normalize-space(category)"/>
    <xsl:template match="/">
        <search-results>
            <result>
                <!-- Find product with SKU: PRD-003 -->
                <xsl:copy-of select="key('sku','PRD-003')"/>
            </result>
            <result>
                <!-- Find all products in Electronics category -->
                <xsl:copy-of select="key('category','Electronics')"/>
            </result>
        </search-results>
    </xsl:template>
</xsl:stylesheet>

Results: products.html

<?xml version="1.0" encoding="UTF-8"?>
<search-results>
   <result>
      <product>
         <sku>PRD-003</sku>
         <name>Office Chair</name>
         <price>149.99USD</price>
         <category>Furniture</category>
      </product>
   </result>
   <result>
      <product>
         <sku>PRD-001</sku>
         <name>Wireless Mouse</name>
         <price>29.99USD</price>
         <category>Electronics</category>
      </product>
      <product>
         <sku>PRD-002</sku>
         <name>Bluetooth Speaker</name>
         <price>79.99USD</price>
         <category>Electronics</category>
      </product>
   </result>
</search-results>

Compared to using the id() function for node location, key() offers greater flexibility and convenience. Additionally, it eliminates the need for external DTD/Schema references.

For more information about the id() function: Understanding the id() Function in XSLT - CSDN Blog

Tags: XSLT XSLT-2.0 xsl-key xpath XML-Transformation

Posted on Sun, 28 Jun 2026 16:47:38 +0000 by redrage