Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I build this XPath query dynamically in XSLT?

Tags:

xml

xslt

xpath

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 <element>.

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?

like image 380
Martin Doms Avatar asked Mar 21 '10 06:03

Martin Doms


People also ask

Does XSLT use XPath?

XSLT uses XPath in three basic ways: To select and match patterns in the original XML data. Using XPath in this manner is the focus of this chapter. You see this most often in <xsl:template match="pattern"> and <xsl:apply-templates select="node-set-expression"/>.

What is the difference between XPath and XSLT?

At bottom, XSLT is a language that lets you specify what sorts of things to do when a particular element is encountered. But to write a program for different parts of an XML data structure, you need to specify the part of the structure you are talking about at any given time. XPath is that specification language.

What is XPath in XSLT?

XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath stands for XML Path Language. XPath uses "path like" syntax to identify and navigate nodes in an XML document. XPath contains over 200 built-in functions.

How do I declare a global variable in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!


1 Answers

  1. You cannot use an XSLT variable as the axis of an XPATH statement, but you can use it in a predicate filter. So, if you were to match on any element (i.e. *) and then restrict it to elements who's name() is equal to the value stored in your variable (i.e. *[name()=$nodename]) the XPATH will be valid.

  2. The XPATH you were constructing would return the value of the matching element. If you want to return how many elements match that pattern, you can use the count() function.

  3. Your example XML has a document element <root>, but your XPATH does not include <root>.

This returns the number of preceding-sibling elements using the variable assigned by the context node:

<xsl:variable name="nodename" select="name()" />
<xsl:value-of select="count(/root/element[1]/*[name()=$nodename]/preceding-sibling::*)" />

You could eliminate the variable and just use:

<xsl:value-of select="count(/root/element[1]/*[name()=name(current())]/preceding-sibling::*)" />
like image 189
Mads Hansen Avatar answered Sep 30 '22 03:09

Mads Hansen