RE: [xsl] Using id() when id and idref are in 2 different files

Subject: RE: [xsl] Using id() when id and idref are in 2 different files
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Mon, 13 Dec 2004 10:49:38 -0000
> So I have tried using document() to read the castlist into a
> variable called $cast. Now I can use <xsl:value-of
> select="$cast//@id" /> to output the required values but I
> can't get it to work with the id()function. <xsl:value-of
> select="id(@who)" /> just fails, which doesn't surprise me.
> However <xsl:value-of select="$cast//role[@id = @who]" />
> also returns nothing. Is it because it is now looking for
> @who within $cast?

Using the id() function relies on the XML getting validated against a
DTD (that specifies that attribute as type ID) - are you sure your
imported files are being validated?  Even then I'm not sure it's
possible...

As an alternative to the id() function, you could use a key:

<xsl:key name="ids" match="*" use="@id"/>

and then access the values:

<xsl:value-of select="key('ids', @who)"/>

Remember to change the context nodes to the referenced XML when you want
to key into that:

<xsl:variable name="who" select="@who"/>
<xsl:for-each select="$cast">
  <xsl:value-of select="key('ids',$who)"/>
</xsl:for-each>

In XSLT 2.0 this can be shortened to:

<xsl:value-of select="key('ids', @who, $cast)"/>

...which is great because you don't need the variable anymore.

cheers
andrew

Current Thread