to the main page about the tutorial  THE XML REVOLUTION  -  TECHNOLOGIES FOR THE FUTURE WEB back up next

Keys

- advanced node IDs for automatic construction of links

A key is a triple (node, name, value) associating a name-value pair to a tree node.

<xsl:key match="pattern" name="..." use="node set expression"/>
declares set of keys - one for each node matching the pattern and for each node in the node set
Extra XPath key function:
key(name expression, value expression)
returns nodes with given key name and value
This is often used together with:
generate-id(singleton node-set expression)
returns unique string identifying the given node

Example:
<xsl:key name="mykeys" match="section[@id]" use="@id"/>

<xsl:template match="section">
  <h1>
    <a name="{generate-id()}">
      <xsl:number/>
      <xsl:apply-templates select="title"/>
    </a>
  </h1>
  <xsl:apply-templates select="body"/>
</xsl:template>

<xsl:template match="ref[@section]">
  <a href="#{generate-id(key('mykeys',@section))}">
    <xsl:for-each select="key('mykeys',@section)">
      Section <xsl:number/>
    </xsl:for-each>
  </a>
</xsl:template>

Comparison to DTD IDs:

back COPYRIGHT © 2000-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH next