gpt4 book ai didi

duplicates - XmlPad 与 XPath 查找重复节点的问题

转载 作者:行者123 更新时间:2023-12-03 00:01:07 25 4
gpt4 key购买 nike

[编辑] 我正在使用的 XML 编辑器 (XmlPad) 中似乎存在一个错误,它会阻止 Xpath 查询返回正确的结果。我已经使用两个在线工具(http://www.zrinity.com/xml/xpath/xpath.cfm 和 http://www.futurelab.ch/xmlkurs/xpath.en.html )测试了相同的查询,它似乎有效。 biziclop 还评论说该查询在 Oxygen 中工作正常。

我有这个结构:

<?xml version="1.0" encoding="utf-8"?>
<root>
<itemlist>
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0002.0.00</code>
<category>708</category>
</item>
</itemlist>
<itemlist>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0016.0.00</code>
<category>51</category>
</item>
<item>
<code>0016.0.00</code>
<category>50</category>
</item>
<item>
<code>0869.0.00</code>
<category>52</category>
</item>
<item>
<code>0869.0.00</code>
<category>51</category>
</item>
<item>
<code>0869.0.00</code>
<category>50</category>
</item>
</itemlist>
</root>

我想找到前一个项目具有相同类别的所有项目。

此 Xpath 查询:

//item[category = preceding-sibling::item[1]/category]

返回以下节点:

<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0869.0.00</code>
<category>52</category>
</item>

结果集中的最后一个项目节点不正确,因为输入中前一个项目的类别值不是 52,因此不应返回。

是否有 Xpath 查询可以返回我想要的结果?

最佳答案

这个 XPath 是绝对正确的。我已经使用 Saxon XSLT 处理器对其进行了测试,如下所示:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:copy-of select="//item[category
= preceding-sibling::item[1]/category]"/>
</xsl:template>

</xsl:stylesheet>

结果:

<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>

您可能还想尝试替代方案:

//item[category = ./preceding-sibling::item[1]/category]
/root/itemlist/item[category = ./preceding-sibling::item[1]/category]

关于duplicates - XmlPad 与 XPath 查找重复节点的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4818119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com