Can I build this XPath query dynamically in XSLT?
Posted
by Martin Doms
on Stack Overflow
See other posts from Stack Overflow
or by Martin Doms
Published on 2010-03-21T06:18:24Z
Indexed on
2010/03/21
6:21 UTC
Read the original article
Hit count: 405
I have a document that looks something like
<root>
<element>
<subelement1 />
<subelement2 />
</element>
<element>
<subelement2 />
<subelement1 />
</element>
</root>
In my XSLT sheet in the context of /element[2]/[someNode] I want to get a number that represents the distance of /element[1]/[someNode] (ie, the number of preceding siblings of /element1/[someNode]). For example, in the context of /element[2]/subelement1 I'd like to have some way to get the number 2, the distance from /element[1] to /element[1]/subelement2. I only ever need the distance of the given node name from the first instance of .
Intuitively I thought I could construct this like
<xsl:variable name="nodename" select="name()" />
<xsl:value-of select="/element[1]/$nodename/preceding-sibling::*" />
but unfortunately this sheet doesn't compile. Is what I'm trying to achieve possible in XSLT?
© Stack Overflow or respective owner