Re: [xsl] Novice question: selecting an element based on an attribute id

Subject: Re: [xsl] Novice question: selecting an element based on an attribute id
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 20 Mar 2002 17:56:51 +0000
Hi Ohmson,

> As you can see, based on "myIdRef", I am supposed to retrieve the
> original string. In this example, the referent and the reference are
> close but sometimes in a big document, they could be far apart. I
> bet I can do this inefficiently by keeping state of all the
> referents in XSL variables but you folks probably have an better
> idea on how to do this efficiently.

Use a key. You can set up a key so that all the elements that have a
myId attribute are indexed by their myId attribute:

<xsl:key name="values" match="*[@myId]" use="@myId" />

And then retrieve an element with a particular value for its myId
attribute using the key() function. For example, to get the element
whose myId attribute is 28, you can use:

  key('values', 28)

If the current node has a myIdRef attribute, then you can use that to
provide the second argument to the call for the key:

  key('values', @myIdRef)

The call to the key() function actually returns the node; you can
apply templates to it, for example, to process it:

  <xsl:apply-templates select="key('values', @myIdRef)" />

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread