Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes XML and Related Technologies and the fly likes preceding-sibling cannot use passed in value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "preceding-sibling cannot use passed in value" Watch "preceding-sibling cannot use passed in value" New topic

preceding-sibling cannot use passed in value

Jay Olsen

Joined: Jan 26, 2008
Posts: 10

I run into a strange problem with preceding-sibling in xslt. The issue occurs when I pass in parameters to an xsl style sheet and try to use it with the preceding-sibling expression.

So we have the following xsl fragment

<td ><xsl:value-of select="preceding-sibling::*[$2]/@VAL"/></td>

which steps back 2 and retrieves the VAL node at that line. This works fine however I now want to pass in the value to step back as a parameter to the xsl style sheet. So the xsl fragment changes to the following with the $index value passed when the script is called.

<td ><xsl:value-of select="preceding-sibling::*[$index]/@VAL"/></td>

and call the style sheet as follows

<param name="index" expression="2"/>

However, the preceding-sibling statement does not work when the value is passed in like this and no step back takes place.

If I print out the value of index in the xsl templates using the following

<xsl:value-of select='$index' />

I get the correct value.

If I declare the index variable in the xsl style sheet itself as follows

<xsl:variable name="index" select="2"/>

it works fine when passed to the preceding-sibling.

What is different between the passed in variable and the global variable declared in the style sheet?

Any help greatly appreciated.

Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Do you mean

(with 2 instead of $2)? If so then I think perhaps the difference is that your parameter is a string, not a number, so your parameterized XPath behaves the same as this one would:

If that's the case then there's a function named something like "number" which converts a string to a number.
Jay Olsen

Joined: Jan 26, 2008
Posts: 10
That is exactly it. The following syntax works for me

<xsl:value-of select="preceding-sibling::*[number($index)]/@VAL"/>

where $index is the value passed into the style sheet.

Thank you very much.
subject: preceding-sibling cannot use passed in value
Similar Threads
Can XSL sheets see Session Vars?
Book Promotion and Past Winners in XSLT
How to get the parent node in XSL?
Why aren't DENSITY & DISTANCE text displayed?
Session variables in XSLT stylesheet