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

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>

Related

Get parents attribute value if child doesn't have a specific attribute value

I have an xml file in linux that I want to process.
I need to get all ids of a parent nodes based on its children.
Here I want to get all id of 'a' that have 'c' without key "f.g".
<a id="11111">
<b>
<c key="d.e">stuff1</c>
<c key="f.g">stuff2</c>
<c key="j.k">stuff4</c>
</b>
</a>
<a id="22222">
<b>
<c key="d.e">stuff1</c>
<c key="h.i">stuff3</c>
<c key="j.k">stuff4</c>
<c key="l.m">stuff5</c>
</b>
</a>
<a id="33333">
<b>
<c key="c.d">stuff0</c>
<c key="d.e">stuff1</c>
<c key="h.i">stuff3</c>
<c key="j.k">stuff4</c>
<c key="l.m">stuff5</c>
</b>
</a>
In this case I should be getting 22222 and 33333.
I'm not really sure how to write the xpath for this.
I think you are looking for something like:
//a[not(.//c[#key="f.g"])]/#id
which can be translated as: find any node <a> which does NOT have a child node <c> which itself has an attribute called key which itself has an attribute value of "f.g".
You can filter by (not):
//a[[not(#key = 'f.g')]]
It will return you needed 'a' elements, but I don't know how to get their ids.
#Jack Fleeting's answer is probably the best solution. As an alternative (more consuming) :
//c[not(#key="f.g" or preceding-sibling::c[#key="f.g"] or following-sibling::c[#key="f.g"])]/ancestor::a
Look for c elements where itself, and preceding or following siblings contain an attribute different from #key="f.g". Then select their a ancestors.

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

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.

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 can I select the first elements in a set of groups?

Consider an Xml document that contains an element <A>. This element contains several elements all named <B>. Each of these elements contains a number of elements.
I want to get a node set, containing the first element in every <B> element, in order of appearance.
You may want to consider this XPath :
//B/*[1]
The first bit of the XPath (//B) looks for all <B> elements. Then from each <B> element, the next bit (/*[1]) will return the first child element.
Sample XML (OP should've been the one who posted an example though, to make his question clearer) :
<A>
<B>
<foo/>
<bar/>
</B>
<B>
<foo/>
<bar/>
</B>
<B>
<foo/>
<bar/>
</B>
</A>
And the output of above XPath in an online xpath tester by freeformatter :
Element='<foo />'
Element='<foo />'
Element='<foo />'
Is this what you are after?
<?xml version="1.0"?>
<root>
<a>
<b><c id="1" /></b>
<b><c id="2" /></b>
</a>
<a>
<b><c id="3" /></b>
<b><c id="4" /></b>
<b><c id="5"/></b>
<b>
<c id="6" />
<c id="7" />
</b>
</a>
<a>
<f><c id="3" /></f>
<b><d id="4" /></b>
</a>
With XPath of
root/a/b[position() = 1]
Resulting in:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<b>
<c id="1"/>
</b>
<b>
<c id="3"/>
</b>
<b>
<d id="4"/>
</b>
</root>
Given an example input XML
<root>
<A>
<B>B 1 1</B>
<B>B 1 2</B>
</A>
<A>
<B>B 2 1</B>
<B>B 2 2</B>
</A>
</root>
the following XPath
//A//B[1]/text()
has the result
B 1 1
B 2 1
as every first B that is a child element of A is selected.
Using //A//B[1] will result in the complete B nodes:
<B>B 1 1</B>
<B>B 2 1</B>

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)

Resources