XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Keys and identifiers / External keys – keys in other documents

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)

Function document() also matters for building key indexes. It can assign keys to external documents during or before processing. When evaluating keys, however, pay attention to the focus of function key():

A key normally returns only nodes in the current node’s document. To evaluate it against another document, first set the context there. Function key() can optionally take a second parameter identifying a document. Alternatively, while this is not implemented, use XSLT function document().

Suppose document keys_buecher.xml contains book-title references in the form <buchreferenz>Mein Buchtitel</buchreferenz>, while another XML document keys_buchdaten.xml is a bibliographic database with entries for all books in this form:

...
<eintrag titel="Haben oder sein">
  <wertung>Das ist ein ...</wertung>
  <zusammenfassung>Der Autor malt ein ausführliches ...</zusammenfassung>
</eintrag>
...
  

A stylesheet resolving the relationship between <buchref> elements in keys_buecher.xml and entries <eintrag> in buchdaten.xml could look like this:

<xsl:key name="ref" match="eintrag" use="@titel"/>
<xsl:template match="buchrefenz">
  <xsl:variable name="der_titel" select="."/>
  <!-- das andere Dokument wird "current": -->
  <xsl:for-each select="document('keys_buchdaten.xml')">
    <xsl:apply-templates select="key('ref',$der_titel)"/>
  </xsl:for-each>
</xsl:template>

Since a key operates only in the current document, first move focus to the other document. Here, xsl:for-each uses function document() to make the whole external document the current node set.

The index can only be built when function key() is called. Instruction xsl:key supplies a blueprint and key name, but no <eintrag> elements or their name attributes were present at declaration time. This does not impair the function.

In XSLT 1.0, note:

Function document() can not be used within a path expression to address external keys, for example:

<!-- In XSLT 1.0 nicht möglich: -->
<xsl:value-of select="document('extern.xml')/key('ref',$der_titel)"/>

Function document() returns the external document node, but the current node remains the one active when xsl:value-of is called. Focus remains in the main document, so the attempt fails. Therefore call docu­ment() in a context that changes the current node.

Keys in imported stylesheets

Declarations xsl:key can also be imported or included with an external stylesheet. The principle of import precedence does not apply here: all keys remain valid. When evaluating with key(), ensure that focus is in the document containing the elements to search. The following general rules apply:

Keys with the same name merge.
Keys can be modular: multiple declarations may share a name. They merge, conceptually concatenating their key lists.

<xsl:key name="star" match="film" use="schauspieler/name"/>
<xsl:key name="star" match="cd" use="musiker/name"/>
<xsl:key name="star" match="sport" use="sportart/name"/>

Name star now identifies one combined list of indexed <film>, <cd> and <sport> elements, which could be queried like this:

<xsl:apply-templates select="key('star', 'Tom Jones')"/>

The result is a node sequence containing CDs and films featuring Tom Jones. If he had also been active in sport, it would find him there too.

Alternative – multi-match keys
If these key definitions are in one stylesheet, and their use-attribute values are identical, a condition we must arrange here, the union operator "|" permits this less precise expression:

<xsl:key name="star" match="film | cd | sport" use="*/name"/>

For greater precision:

<xsl:key name="star" match="film | cd | sport" use="name/parent::node()[name()='schauspieler' or name()='musiker' or name()='sportart']"/>

Now abandon the idea that all these xsl:key declarations must share a stylesheet module: they need not! Same-name key lists are always added, regardless of declaration location. A local declaration is equivalent to an external one.

   

back to top