File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 19973

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.
I agree. Here's the link:
subject: preceding-sibling cannot use passed in value
It's not a secret anymore!