简体   繁体   English

XPath表达式有条件地获取相邻节点

[英]XPath Expression to get adjacent nodes conditionally

I have an xml file with some content like the following: 我有一个包含以下内容的xml文件:

<p>
    <r>
        <t xml:space="preserve">Reading is easier, </t>
    </r>
    <r>
        <fldChar fldCharType="begin"/>
    </r>
    <r>
        <instrText xml:space="preserve"> REF _Ref516568558 \r \p \h </instrText>
    </r>
    <r>
        <fldChar fldCharType="separate"/>
    </r>
    <r>
        <t>This is all the text I want to capture</t>
    </r>
    <r>
        <fldChar fldCharType="end"/>
    </r>
    <r>
        <t xml:space="preserve">, in the new Reading view </t>
    </r>
    <r>
        <fldChar fldCharType="begin"/>
    </r>
    <r>
        <instrText xml:space="preserve"> REF _Not516755367 \r \h </instrText>
    </r>
    <r>
        <fldChar fldCharType="separate"/>
    </r>
    <r>
        <t>But not this...</t>
    </r>
    <r>
        <fldChar fldCharType="end"/>
    </r>
    <r>
        <t xml:space="preserve"> Some other text... </t>
    </r>
</p>

I know that I can use the XPath expression //instrText[contains(text(), '_Ref')] to get <instrText xml:space="preserve"> REF _Ref516568558 \\r \\p \\h </instrText> . 我知道我可以使用XPath表达式//instrText[contains(text(), '_Ref')]来获取<instrText xml:space="preserve"> REF _Ref516568558 \\r \\p \\h </instrText>

Now what I want to get is the text within t nodes between <fldChar fldCharType="begin"/> and <fldChar fldCharType="end"/> if between these two tags there is a instrText with text that contains '_Ref' ie instrText[contains(text(), '_Ref'] . 现在,我想得到的是<fldChar fldCharType="begin"/><fldChar fldCharType="end"/>之间的t节点中的文本,如果这两个标记之间有一个包含'_Ref'instrText文本,即instrText[contains(text(), '_Ref']

Based on this, from the example xml, I would expect only: <t>This is all the text I want to capture</t> to be returned. 基于此,在示例xml中,我只会期望: <t>This is all the text I want to capture</t>

Can this be done with a single XPath 1.0 expression? 可以使用单个XPath 1.0表达式完成此操作吗?

试试这个: p/r[preceding-sibling::r[fldChar/@fldCharType='begin'] and following-sibling::r[fldChar/@fldCharType='end']]/t[contains(., '_Ref')]

这就是我最终使用的内容: //p/r[preceding-sibling::r[fldChar/@fldCharType='begin'] and following-sibling::r[fldChar/@fldCharType='end']][instrText[contains(text(), '_Ref')]]/following-sibling::r[t][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM