Re: [xsl] extract the right elements

Subject: Re: [xsl] extract the right elements
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 30 Sep 2003 12:52:47 +0100
Hi Clemens,

> <xsl:for-each select="//INSTANCE[@class='Audio' or @class='Web-Site']">
>     <xsl:for-each select="key('tracker','Position')">
>         <item>
>             <xsl:attribute name="identifierref">
>                 <xsl:value-of select="."/>
>             </xsl:attribute>
>             <title>
>                 <xsl:value-of select="parent::INSTANCE/@name"/>
>             </title>
>         </item>
>     </xsl:for-each>
> </xsl:for-each>

The key() function returns the nodes with a certain value for a
particular key. In the above, key('tracker', 'Position') will always
return the same node-set: all the nodes that, on the 'tracker' key,
have the value 'Position'. So you get an <item> for each of those
nodes, for each of the <INSTANCE> elements whose class is 'Audio' or
class is 'Web-Site'.

> the problem is, that i don't want to use it on all "instance"
> elements, but only on those whose class attributes are either
> 'Audio' or 'Web-Site', but the first for-each loop that is meant to
> fulfill this purpose simply doesn't work, instead it always runs the
> loop for all "instance" elements.

It sounds as though you want to filter the elements that are being
returned by the key, to only include those whose parent <INSTANCE>
element has a class of 'Audio' or 'Web-Site'. In that case, you need
to apply the filter (the predicate) to the node-set returned by the
key() function, as follows:

  <xsl:for-each select="key('tracker', 'Position')
                          [parent::INSTANCE/@class = 'Audio' or
                           parent::INSTANCE/@class = 'Web-Site']">

On the other hand, I'm not sure why you're using the key() at all
here. I think that you could get the result that you want (from the
previous thread) with:

  <xsl:for-each select="INSTANCE[@class = 'Audio' or
                                 @class = 'Web-Site']">
    <item identifier="{ATTRIBUTE[@name = 'Position']}">
      <xsl:value-of select="@name" />



Jeni Tennison

 XSL-List info and archive:

Current Thread