Xpath select link xml node

I need to select a node whose "name" attribute is equal to the current node "attribute.

<node name="foo" />
<node name="bar" references="foo" />

      

I couldn't find a way to express this in XPATH. Any ideas. I've tried the following:

./node[@name=@references]
./node[@name={@references}]

      

Obviously it didn't work. I'm guessing the real problem is in parentheses; how can I tell which attribute is coming from which node?

+2


source to share


3 answers


Unfortunately what you are trying is not possible with pure XPath. Whenever you start a new predicate (the part surrounded by brackets), the context changes to the node that started the predicate. This means that you cannot directly compare attributes from two separate elements in the same predicate without storing them in a variable.

What language are you using? You will need to store the value of the "name" attribute from the first node in a variable.

For example, in XSLT:



<xsl:variable name="name" select="/node[1]/@name" />
<xsl:value-of select="/node[@references = $name]" />

      

In XQuery

let $name := /node[1]/@name
return /node[@references = $name]

      

+2


source


I'm not really sure if this is what you want. This gives you a node that has links from any node to it:



//node[@name=//node/@references]

      

+3


source


Natural solution:

node[@name = current()/@references]

      

This works in XSLT because you are talking about "current node", which I translate as "XSLT context node". No extra variable needed.

+1


source







All Articles