Re: [xsl] Dynamic xsl:value-of

Subject: Re: [xsl] Dynamic xsl:value-of
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 26 Apr 2002 12:44:21 +0100
Hi Jonas,

> I need to use the "loop-counter" to get an attribute value. Sounds
> strange, huh? Here's a simplified example:
>
> Part of the XML:
>
> <db2000>
> <http>
> <param name="type0" value="xxx"/>
> <param name="type1" value="yyy"/>
> <param name="name0" value="yyy"/>
> <param name="name1" value="xxx"/>
> </http>
> </db2000>
>
> I have a xslt-template with something like:
>
> <xsl:template match="blabla">
>    <xsl:value-of
> select="/db2000/http/param[@name='type{position()}]/@value/>
> </xsl:template>
>
> The problem is that the syntax don't work :)
>
> Any ideas on how I can construct the xpath expression dynamically?

The XPath that you're using there isn't valid. It looks like you're
trying to use an attribute value template within the XPath, and you
can't do that. Instead you should use concat() to concatenate the
string 'type' with the number returned by the position function:

  /db2000/http/param[@name = concat('type', position())]/@value

You haven't described what you want the result of the XPath to be, but
this path will give you the values of the param elements whose name is
equal to 'typeX' where X is the position of the param element within
the http element. This cannot be true for any of the param elements in
your example -- the first has position 1, but name 'type0', the second
position 2, but name 'type1' and so on.

I guess that you might be trying to get the one whose name is 'typeX'
where X is the position of the current blabla element? In that case,
you need to store the position of the current blabla element in a
variable, and then refer to that variable:

<xsl:template match="blabla">
  <xsl:variable name="X" select="position()" />
  <xsl:value-of
    select="/db2000/http/param[@name = concat('type', $X)]/@value" />
</xsl:template>

[Example variable name dedicated to David C.]

Remember also that in XPath, positions start from 1 rather than 0.

If this doesn't do what you want, let us know what you want the result
of the template or XPath to be, and we should be able to help further.

Cheers,

Jeni

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


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


Current Thread