web analytics

<xsl:key> Element and key() Function in XSLT

Options

codeling 1599 - 6654
@2016-01-24 10:53:01

In XSLT, the <xsl:key> element is a top-level element which declares a named key that can be used in the style sheet with the key() function which returns a node-set from the XML document, using the index specified by an <xsl:key> element.

<xsl:key name="keyName" match="pattern" use="expression"/>

key("keyName", "expressionValue")

Each <xsl:key> element can only be a child of the xsl:stylesheet or the xsl:transform elements. It cannot contain any elements as content, nor can it appear inside a template.

The appropriate use of these key-value pairs can permit easy access to information in complex XML documents efficiently.

For example:

<?xml version="1.0"?>
<!-- This XML docuemnt is used to define a book -->
<book isbn="5CC60E5B-A25C-4aeb-B158-3998EE660FCD">
    <title>XSLT</title>
    <author>James Renaud</author>
    <publishDate>2007/11/10</publishDate>
    <publisher>ABC Book Company</publisher>
    <price currency="USD">28.88</price>
    <!-- Chapter 1 -->
    <chapter id="1">
        <title>Getting Started</title>
        <!-- Paragraph 1 in chapter 1 -->
        <para>1.1 para.</para>
    </chapter>
    <chapter id="2">
        <title>The Hello World Example</title>
        <para>2.1 para.</para>
        <para>2.2 para.</para>
        <para>2.3 para.</para>
    </chapter>
    <chapter id="3">
        <title>XPath</title>
        <para>3.1 para</para>
    </chapter>
    <chapter id="4">
        <title>Stylesheet Basics</title>
        <para>4.1 para.</para>
        <para>4.2 para.</para>
        <para>4.3 para.</para>
        <para>4.4 para.</para>
    </chapter>
    <chapter id="5">
        <title>Branching and Control Elements</title>
        <para>5.1 para.</para>
    </chapter>
    <chapter id="6">
        <title>Functions</title>
        <para>6.1 para.</para>
    </chapter>
    <chapter id="7">
        <title>Creating Links and Cross-References</title>
        <para>7.1 para.</para>
        <para>7.2 para.</para>
    </chapter>
    <chapter id="8">
        <title>Sorting and Grouping Elements</title>
        <para>8.1 para.</para>
    </chapter>
    <chapter id="9">
        <title>Combining XML Documents</title>
        <para>9.1 para.</para>
    </chapter>
</book>

The following stylesheet uses <xsl:key> element to produce a title-chapter pair list, then uses key() function to find a specific chapter element by its title. 

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="chapterList" match="chapter" use="title" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('chapterList', 'Combining XML Documents')">
  <div>
     <xsl:value-of select="@id"/>
     <xsl:text> </xsl:text>
     <xsl:value-of select="./title" />
  </div>
</xsl:for-each>
</body>
</html>
</xsl:template>

</xsl:stylesheet>  
@2016-03-03 21:46:28

When you work with relatively large XML documents, a key is often a fast way to query elements. This is because it essentially indexes the locations of the desired nodes ahead of time. However, when <xsl:key> elements are compiled, the XSLT processor will not create such indexes unless a key() function is called against the keys. This ensures that the time-intensive indexing operation is performed only when it is necessary.

In a stylesheet, <xsl:key> elements are top-level elements, and cannot appear within a template. To avoid circular references, you cannot use parameter or variable references as part of an <xsl:key> match.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com