Re: [xsl] accessing specific elements

Subject: Re: [xsl] accessing specific elements
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 8 May 2001 09:14:45 +0100
Hi Sabine,

> -     count the tokens which have a domlex[@attribute='location']
>         (this is no problem, I just use
> count(//domlex[@attribute='location'])"/>))
>
> -     access only those tokens with an [@attribute='location'],  so that
> I can express
>         the condition: "If the first element with  @attribute='location'
> and the third have the same
>         value, then write out only one as the origin (and the middle one
> as the destination).
>
> I thought of storing the value of the first in a variable and
> comparing it with subsequent ones... but I can't get it work...

You can store them in a variable like:

<xsl:variable name="locs"
  select="/input_stream/chunk/token/domlex[@attribute = 'location']" />

(I step down from the root rather than using the descendant axis as
it's likely to be more efficient - you might want to check the path to
make sure it's right for your source XML.)

The $locs variable now contains three nodes - the three domlex
elements with the @attribute attribute equal to 'location'.  When you
index into a node set like this, then their positions (as indicated in
positional predicates like [1]) are relative to the other nodes in the
node set.  So you can get the first with $locs[1], the second with
$locs[2] and the third with $locs[3].

So you should be able to use the following to get the kind of output
that you're after.

   Origin: <xsl:value-of select="$locs[1]/@value" />
   <xsl:choose>
      <xsl:when test="$locs[1]/@value = $locs[3]/@value">
         Destination: <xsl:value-of select="$locs[2]/@value" />
      </xsl:when>
      <xsl:otherwise>
         Via: <xsl:value-of select="$locs[2]/@value" />
         Destination: <xsl:value-of select="$locs[3]/@value" />
      </xsl:otherwise>
   </xsl:choose>

I hope that helps,

Jeni

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



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


Current Thread