问题
I have the following XML:
<MyFruits>
<Apple>A</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Apple>19</Apple>
<Orange>A</Orange>
<Orange>A</Orange>
<Orange>A</Orange>
<Apple>B</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Apple>19</Apple>
<Orange>10</Orange>
<Apple>19</Apple>
<Orange>10</Orange>
<Apple>C</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Apple>19</Apple>
<Orange>10</Orange>
<Apple>19</Apple>
<Orange>10</Orange>
</MyFruits>
I want to get only unique fruits among them (Apple,Mango and Orange)
Can anyone suggest an XPath to retrieve using XPath 1.0?
Thanks
回答1:
I don't think this can be done in a single XPath 1.0 expression because it would require two nested levels of predicates with the inner one depending on the value of the outer one, i.e.
/MyFruits/*[not(preceding-sibling::*[name() = name(node-from-outer-predicate)])]
and this isn't possible in XPath 1.0.
It would be trivial in XPath 2.0
distinct-values(/MyFruits/*/name())
来源:https://stackoverflow.com/questions/24779946/xpath-for-getting-unique-values-of-node-names