It should return all first occurrences for nodes with same name, even if the childs and attribbutes differ.
For example
<Data>
<A>
<X randomattr="1"/>
<Y randomattr="1"/>
<Z/>
</A>
<B>
<X/>
<X randomattr="3"/>
<Z/>
</B>
</Data>
It sould return 3 nodes, first X, Y and Z, because the following, will have a repeated name. Don’t mind if one of the X elements does not have randomattr or if another has a different value.
I dont want the distinct-values from name(), I want to return the whole node. Something like
/Data/*/*[distinct-values(name())]
I also know I can transverse all nodes with a double loop, but I am asking myyself if there is an easy one-liner or a function for this, or a special Xpath syntax like distinct[1]
Thank u!
I think this is what you might be looking for. Worked with some simple tests:
/Data/*/*[not((preceding-sibling::*/name(.)=name(.)) or (../preceding-sibling::*/*/name(.)=name(.)))]/Data/*/*[not((name(preceding-sibling::*)=name(.)) or (name(../preceding-sibling::*/*)=name(.)))]After the request to make it work to any depth… I am unfamiliar with your schema, but from the example I’m assuming that the nodes you are looking for are the bottom level nodes. The following uses the descendant axes to search for nodes with no children while doing the name check.
/Data/descendant::*[not(name() = preceding::*[not(child::*)]/name())][not(child::*)]