我用完了如何避免这种“重复”节点的情况。
问题: 考虑一下,我们遵循xml:
<root>
<cities>
<city id = "1">
<name>ABC</name>
<value>1</value>
</city>
<city id = "2">
<name>DEF</name>
<value>2</value>
</city>
<city id = "1">
<name>ABC</name>
<value>1</value>
</city>
<city id = "1">
<name>ABC</name>
<value>1</value>
</city>
<cities>
</root>
我需要存档的内容是避免重复。所以在输出上我想看到并输出2个节点:
<city id = "1">
<name>ABC</name>
<value>1</value>
</city>
<city id = "2">
<name>DEF</name>
<value>2</value>
</city>
我在发布之前尝试了很多解决方案(如//*:city[@name='ABC']/@name
)
但它返回包含所有重复项的列表。(在本例中为3)
任何想法如何解决?
非常感谢
答案 0 :(得分:1)
使用示例HTML | XML
测试以下XPaths
here...
<cities>
<city>
<name>ABC</name>
<value>2</value>
</city>
<city>
<name>ABC</name>
<value>1</value>
</city>
<city>
<name>DEF</name>
<value>2</value>
</city>
<city>
<name>AB</name>
<value>1</value>
</city>
<city>
<name>ABC</name>
<value>1</value>
</city>
<cities>
来自子元素列表的不同数据:
//cities/descendant::city[not(.=preceding-sibling::city)]
不同节点Values
形成父City
兄弟姐妹。
//cities/descendant::city[not(./value=preceding-sibling::city/value)]/value
来自父Names
兄弟姐妹的不同节点City
。
//cities/descendant::city[not(./name=preceding-sibling::city/name)]/name
nodes of same level
包含不同的数据。
<root>
<item type='test'>A</item>
<item type='test'>B</item>
<item type='test'>C</item>
<item type='test'>A</item>
<item type='other'>A</item>
<item type='test'>B</item>
<item type='other'>D</item>
</root>
//preceding::item/preceding::item[not(.=preceding-sibling::item)]/text()
使用节点属性值更新了答案:
<root>
<cities>
<city id = "1">
<name>ABC</name>
<value>1</value>
</city>
<city id = "3">
<name>DEF</name>
<value>2</value>
</city>
<city id = "1">
<name>ABC</name>
<value>1</value>
</city>
<city id = "4">
<name>ABC</name>
<value>1</value>
</city>
<city id = "2">
<name>DE</name>
<value>2</value>
</city>
<cities>
</root>
的XPath:
//cities/city/following::city[not(./@id=following::city/@id)] //cities/city/following::city[not(./@id=following::city/@id)]/name //cities/city/following::city[not(./@id=following::city/@id)]/value