两个元素之间的 XPath [英] XPath between two elements
问题描述
我有一个 Word 2003 XML 文档,我正在尝试在其中搜索某些元素.我已经能够执行简单的 XPath 查询来查找单个元素,但是我很难提出在两个元素之间进行搜索的查询:
I have a Word 2003 XML document that I am trying to search for certain elements in. I have been able to do simple XPath queries to find single elements, but I am having difficulty coming up with a query to search between two elements:
<w:r>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:instrText> DOCPROPERTY EvidenceBase \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:t>EvidenceBase</w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
我正在搜索上述 XML,其中有一个 w:r,其中有一个 w:fldChar,其属性为 w:fldCharType,值为begin".它应该返回每个元素,直到它遇到一个带有 w:fldChar 的 w:r,它的属性为 w:fldCharType,值为end".
I am searching for the above XML, that has a w:r with a w:fldChar in it which has an attribute of w:fldCharType with value of "begin". It should return every element until it hits a w:r with a w:fldChar in it which has an attribute of w:fldCharType with value of "end".
这可能吗?
推荐答案
//w:r[preceding-sibling::w:r[w:fldChar/@w:fldCharType='begin'] and following-sibling::w:r[w:fldChar/@w:fldCharType='end']]
请注意前缀 w
需要绑定到 XPath 表达式命名空间上下文的正确命名空间.如何完成取决于您如何使用 XPath(XSLT、Java、C#...).
Mind that the prefix w
would need to be bound to the proper namespace for the XPath expression namespace context. How this is done depends on how you use the XPath (XSLT, Java, C#...).
此外,如果有多个可能嵌套的开始"和结束"标记,这会更复杂.
Also, this would be more complex if there's multiple, possibly nested "begin" and "end" markers.
这篇关于两个元素之间的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!