XPath / XQuery: find text in a node, but ignoring content of specific descendant elements

前端 未结 4 1730
星月不相逢
星月不相逢 2021-02-05 18:36

I am trying to find a way to search for a string within nodes, but excluding ythe content of some subelements of those nodes. Plain and simple, I want to search for a string in

相关标签:
4条回答
  • 2021-02-05 19:08

    For the record, as a complement to the other answers, I've found this workaround that also seems to do the job:

    //p[contains(child::text()|not(descendant::footnote), "text")]
    
    0 讨论(0)
  • 2021-02-05 19:09

    I want to search for a string in paragraphs of a text, excluding the footnotes which are children elements of the paragraphs

    An XPath 1.0 - only solution:

    Use:

    //p//text()[not(ancestor::footnote) and contains(.,'text')]
    

    Against the following XML document (obtained from yours but added p s within a footnote to make this more interesting):

    <document>
        <p n="1">My text starts here/</p>
        <p n="2">Then it goes on there
            <footnote>It's not a very long text!
               <p>text</p>
            </footnote>
        </p>
    </document>
    

    this XPath expression selects exactly the wanted text node:

    My text starts here/
    
    0 讨论(0)
  • 2021-02-05 19:12

    /document/p[text()[contains(., 'text')]] should do.

    0 讨论(0)
  • 2021-02-05 19:24
    //p[(.//text() except .//footnote//text())[contains(., 'text')]]
    
    0 讨论(0)
提交回复
热议问题