Once again about the distinct nodes (basing on attribute values) in node set. Imagine u have the following structure:
<struct>
<a>
<x id="1">a_1</x>
<x id="2">a_2</x>
<x id="3">a_3</x>
</a>
<b inherits="a">
<x id="2">b_2</x>
</b>
</struct>
<struct/>
may contain multiple elements like <b/>
which inherit the same <a/>
. At the same time multiple elements like <a/>
are allowed. Order of <a/>
s and <b/>
s is arbitrary. Inheritance is single-level deep.
Question: How to create a single XPath that selects the following nodeset for a given <b/>
:
<x id="1">a_1</x>
<x id="2">b_2</x>
<x id="3">a_3</x>
Please note the b_2
value on the second line.
Any solutions to this?
Update:
The resuting XPath should have the following form: b[(magic_xpath)[@id=2]='b_2']
, where magic_xpath
selects distinct <x/>
s from <a/>
s and <b/>
s.
The real life <struct/>
can look like this:
<struct>
<a>
<x id="1">a_1</x>
<x id="2">a_2</x>
<x id="3">a_3</x>
</a>
<b inherits="a">
<x id="2">I don't match resulting XPath</x>
</b>
<b inherits="a">
<x id="2">b_2</x>
</b>
</struct>
Use:
where
$vB
is defined to be theb
element.This selects the union of all
b/x
elements and allx
children (withid
attribute that isn't equal to anyb/x/@id
attribute) ofstruct/*
elements (children ofstruct
) whose name is the value ofb/@inherits
.Or, as requested by the OP in a comment -- without variables:
Complete XSLT-based verification:
when this transformation is applied on the provided (corrected to be well-formed) XML document:
the single XPath expression is evaluated and the selected nodes are output: