Xpath for getting unique values of node names

妖精的绣舞 提交于 2019-12-11 06:37:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!