Why doesn't //* return the document node? - xpath

I am trying to understand the following example
<?xml version="1.0" encoding="UTF-8"?>
<c>
<a>
<b att1="5">
<c/>
</b>
<d/>
</a>
<a att1="10">
<d>
<c/>
</d>
<b/>
</a>
</c>
Now I run the XPath query
//*[c]
which I take to mean "All nodes that have a child that is a c". However, this returns only the <b> and <d> nodes that have a <c> child without returning the Document node as I expected. Can anyone explain why?

Because //* equivalent to /descendant-or-self::node()/*. Notice that the document node referenced by self::node() in the previous XPath, so the outer most node selected by that XPath would be the child of the document node (due to /*), which is the root element c, which doesn't have direct child c, hence didn't get selected.
You want /descendant-or-self::node()[c] to include the document node, which is equivalent to //.[c], see the demo.

Related

XPATH / ElementTree - How to get next element that is not a child

i want to search for a specific element <B> if <B>'s child <C> equals the string s1 i want to search from that element on to the next element <X> that is NOT a child of <B> and return its value (s2).
The tree would look something like this:
<A>
<B>
<C>s1</C>
</B>
<D>
<X>s2</X>
</D>
</A>
The following works for me in xsh
//X[preceding::B[C='s1']][not(parent::A)]/text()

xpath: select node closest to root

I need select a specific node name closest to (not needly on) root.
Example:
<root>
<a>
<b id="1"></b>
<b id="2">
<b id="3"></b>
</b>
<c>
<b id="4"></b>
</c>
</a>
</root>
It should select b#1, b#2 and b#4, but not b#2, because it is included inside of another b node.
Currently I'm doing that: select all b, so check if some of parents is b, if yes, discard that. But I do it hardcoded, maybe xpath can solve that alone?
I found the solution, just using not + ancestor, like:
//table[not(ancestor::table)]
I would try below expression-
//b[not(.//ancestor::b)]
It selects-
<b id="1"/>
<b id="4"/>
See live at here.

How to group two nodes which are not related in xpath?

I have html structure like this:
<a>
<c>
</c>
</a>
<b>
<d>
</d>
</b>
<a>
<c>
</c>
</a>
<b>
<d>
</d>
</b>
How do I group node 'a' and node 'b' together?
The xpath should be able to select the pairs of node 'a' and 'b'.
The nodes have auto generated id's and name's so I can't use them in xpath.
You can use the | operator for two unrelated XPath Expressions:
(//a | //b)

Web config transformation condition/match to select a node based on parent node attribute

I have a transform that looks like this
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<a>
<b>
<c>
<d>
<e name="UpdateLanguageProfile">
<f xdt:Transform="Replace" xdt:Locator="Condition(/..#name='UpdateLanguageProfile')">
stuff here
</f>
</e>
</d>
</c>
</b>
</a>
So I want the xdt:Locator to select the f node only if the parent node has an attribute with the specified value.
The xdt:Locator gets translated into the following xpath expression:
/a/b/c/d/e/f[/..#name='UpdateLanguageProfile']
Which is invalid.
So the question is, what could I put in the Condition, that is the XPath square brackets, in order to select the f node based on an attribute in the parent node.
The answer is that the xdt:Locator and the xdt:Transform do not need to be on the same node. They just happen to be on the same node in every example I've ever seen.
You can do this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<a>
<b>
<c>
<d>
<e name="UpdateLanguageProfile" xdt:Locator="Match(name)">
<f xdt:Transform="Replace">
stuff here
</f>
</e>
</d>
</c>
</b>
</a>

XPath: limit scope of result set

Given the XML
<a>
<c>
<b id="1" value="noob"/>
</c>
<b id="2" value="tube"/>
<a>
<c>
<b id="3" value="foo"/>
</c>
<b id="4" value="goo"/>
<b id="5" value="noob"/>
<a>
<b id="6" value="near"/>
<b id="7" value="bar"/>
</a>
</a>
</a>
and the Xpath 1.0 query
//b[#id=2]/ancestor::a[1]//b[#value="noob"]
The Xpath above returns both node ids 1 and 5. The goal is to limit the result to just node id=1 since it is the only #value="noob" element that is a descendant of the same <a> that (//b[#id=2]) is also a descendant of.
In other words, "Find all b elements who's value is "noob" that are descendants of the a element which also has a descendant whose id is 2, but is not the descendant of any other a element". How's that for convoluted? In practice the id number and values would be variable and there would hundreds of node types.
If the id=2, we would expect to return element id=1 not id=5 since it is contained in another a element. If the id=4, we would expect to return id=5, but not id=1 since it is not in the first ancestor a element as id=4.
Edit:
Based on the comments of Dimitre and Alejandro, I found this helpful blog entry explaining the use of count() with the | union operator as well as some other excellent tips.
Use:
//b[#value='noob']
[count(ancestor::a[1] | //b[#id=2]/ancestor::a[1]) = 1]
Explanation:
The second predicate assures that both b elements have the same nearest ancestor a.
Remember: In XPath 1.0 the test for node identity is:
count($n1 | $n2) = 1
First, this
is there some way to limit the result
set to the <b> elements that are ONLY
the children of the immediate <a>
element of the start node
(//b[#id=2])?
//b[#value='noob'][ancestor::a[1]/b/#id=2]
It's not the same as:
Starting at a node whose id is equal
to 2, find all the elements whose
value is "noob" that are descendants
of the immediate parent c element
without passing through another c
element
Wich is:
//c[b/#id=2]//*[.='noob'][ancestor::c[1][b/#id=2]]
Besides these expressions, when you are dealing with "context marks" you can use the set's membership test as in:
$node[count(.|$node-set)=count($node-set)]
I leave you its use for this case as an exercise...
//b[#id=2]/ancestor::a[1]//b[#value="noob" and not(ancestor::a[2]=//b[#id=2]/ancestor::a[1])] ?
that works only for your case though, not sure how generic it should be!

Resources