xml - Xpath - 根据前 sibling 和后 sibling 选择节点

标签 xml xpath xslt-2.0

我有一个这样的xml,

<doc>
    <p><c type="changeStart"/>​<style type="underline">text</style><c type="changeEnd"/><t/>In addition 
        to voting Finance Company and Business Company, Inc.: (i) the name 
        of the <c type="changeStart"/>new public entity<c type="changeEnd"/> will be “Finance Company.” 
        as Finance Company’s corporate existence is perpetual as opposed to Business Company, Inc.’s corprate 
        existence terminating <c type="changeStart"/>   <c type="changeEnd"/>
    </p>
</doc>

我需要选择 <c type="changeStart"/> 之间存在的节点和 <c type="changeEnd"/> .所以在上面的xml中应该选择以下节点,

  • <style type="underline">text</style>
  • new public entity
  • 两个空格(' ')

我为此编写了以下 xpath,

//*[preceding-sibling::c[@type = 'changeStart'] and following-sibling::c[@type = 'changeEnd']][not(c [@type="changeStart"])]

但它没有选择正确的节点。有什么建议可以修改我的 xpath 以选择我需要的内容吗?

最佳答案

这是一种可能的 XPath:

//node()[
    preceding-sibling::*[1][self::c/@type='changeStart']
        and
    following-sibling::*[1][self::c/@type='changeEnd']
]

上面的 XPath 选择节点:

  • 紧接 <c type="changeStart"/> , 和..
  • 紧跟 <c type="changeEnd"/>

关于xml - Xpath - 根据前 sibling 和后 sibling 选择节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501661/

相关文章:

java | XML 按大小分割 | HashMap 性能问题 | OOM 堆空间错误

android - Android 中的 Xml 或文本声明不是以实体开头

ios - 在iPhone中使用NSXmlParser时如何获取xml中标记的路径?

javascript - 在 javascript 中使用 xPath 解析具有默认命名空间的 XML

XSLT 2.0,检查特定位置是否存在特定节点

xml - XSLT 生成文件夹

java - OpenID Connect 的 Spring Security 5 XML 配置

css - 在 Google 搜索上查找 "Next"按钮的正确 xpath

mysql - 计算 Extract Value 找到匹配项的行数

c# - 使用 .NET 基于属性在 XSLT 中选择元素