Re: [xsl] xsl:choose and multiple (sibling) elements

Subject: Re: [xsl] xsl:choose and multiple (sibling) elements
From: "Jon Gorman" <jonathan.gorman@xxxxxxxxx>
Date: Thu, 13 Apr 2006 09:44:22 -0500
Hi Alex,

> i need to get the value (here--> 'BBEZ ist vorhanden') of the <inhalt> tag
> of the last <element>, where <belegBereich> has the value BBEZ. otherwise
> (if such an <belegBereich> doesn't exist) , the text should be retrieved
> elsewhere in the xml.
> here is the relevant part of my xsl:

I think we need to know what the current node is at this point.  From
the code below I'd guess either: / or element.  It's hard to guess.
Does the template that this belong in have
<xsl:template match="/" or <xsl:template match="element".

If it's the first case, you seem to be confusing how the context node

First, you want to see if the last element has the proper string value...
this is a quick and easy setup (you might need more refactoring but
let's just do this for now).
<xsl:when test="inhaltsListe/element[position() = last() and
belegBereich = 'BBEZ']">
<xsl:value-of select="inhaltsListe/element/textListe/element/inhalt">
... rest of choose

Of course, if you're at an element node I don't know how you're
imposin the "last" criteria.  Perahaps you're assuming that there is
at least one element.  If so:

<xsl:when test="belegBereich = 'BBEZ'">
<xsl:value-of select="textListe/element/inhalt">

>                         <xsl:choose>
>                                 <xsl:when test="//belegBereich = 'BBEZ'">
Why are you using //?  That means you're looking for a belegBereich
with the string value of BBEZ in either self or descendants of the
current node.  If that's /, it's the whole tree.  It's horribly
ineffiecient and rarely necessary.  This also doesn't give me any clue
of the current node.  In additions to the samples above, if the
current content node is belegBereich, why not just test=". = 'BBEZ'"?

>                                         <!--xsl:value-of
> select="./textListe/element/inhalt"/-->
If the current node is element this should do just fine, although the
./ is  not needed.
>                                         <xsl:value-of select="
> ../textListe/inhalt"></xsl:value-of>
Here's where I'm confused a little.  Perhaps we're at the level of
BBEZ.  If so, then the path should be ../textListe/element/inhalt,

> doesn't work, i hoped that the ../inhalt would lead the processor to <
> inhalt>BBEZ ist vorhanden</inhalt>, since <textListe> is also a child of <
> element> as <belegBereich> is. didn't work.

It's entirely dependent on your current node.  It looks like it could
be a simple mistake of leaving out the element or much larger mistakes
with confusion over the nature of what the current node is.

> i tried many different approaches, including to wrap a xsl:for-each (with
> an absolute path until <element>) around the xsl:choose, which ended up in
> printing the desired text 8 times next to each other (= number of
> <element> tags)... but at least

probably because of the path you chose, had you just done

<xsl:for-each select="element[position() = last()]" you would have
only selected one.  This is also why I'm not entirely positive you
made a simple path mistake or you're confusing how the current node
works.  (It changes after a for-each since you're working on each
element in turn, where in a if you're merely testing but not changing
the current node.

Jon Gorman

Current Thread