Re: [xsl] xpath dynamic selection

Subject: Re: [xsl] xpath dynamic selection
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 11 Jul 2004 10:09:55 +0100
Hi Wim,

I found it really quite difficult to follow your question. It would
help if you could show an small sample of your input XML and the
output that you'd like to generate from it, plus all the relevant
code. I'll try to answer your questions, though:

> When I look at all the examples every selection like
> select="child::item[@value>3]" has a constant value to test against ("3" in
> this case).
> Can't this be done with a variable? eg: 
> select="child::item[@value>$basevalue]"

Yes, it can.

> It try to do this by calling the alts in item with:
>   	<xsl:apply-templates match="alt">
> 	  <xsl:with-param name="step" select="$step"/>
> 	</xsl:apply-templates>
> with the $step number of the item
> and in alternative:
>     <xsl:param name="start">
> 	<xsl:value-of select="ancestor::*/*/alt[@number=$anumber]"/>
>     </xsl:param>    
> with $anumber the number of the alternative.
> But it returns nothing.

You haven't shown us how the $anumber parameter gets assigned its
value. Perhaps it's not getting set to the number that you think it's
getting set to?

> If I remove the "[@number=$anumber]" part it always returns 1, so I
> guess it returns the first match.

You're right that <xsl:value-of> always returns the value of the first
node that you select. Usually, when setting a variable or parameter,
it's a good idea to use the select attribute so that it holds all the
nodes that you select, as in:

  <xsl:param name="start"
    select="ancestor::*/*/alt[@number=$anumber]" />

I'm not sure that the path you're using is the one you want here; from
the sketch of the tree that you gave (though you described it as the
output rather than the input), perhaps it should be:

  <xsl:param name="start"
    select="../../alt[@number=$anumber]" />

If the <alternative> element is the current element, and you want the
<alt> element whose number attribute has the same value as the number
attribute on the current <alternative> element, then you could use:

  <xsl:param name="start"
    select="../../alt[@number=current()/@number]" />


> I also don't understand how I can get the @step value from the alt.
> adding /attribute::step after the */*/alt doesn't seem to work.

Without seeing your input XML and your XSLT code, it's impossible to
tell why this doesn't work.



Jeni Tennison

Current Thread