XPath find text in any text node

前端 未结 1 1537
深忆病人
深忆病人 2020-12-02 20:20

I am trying to find a certain text in any text node in a document, so far my statement looks like this:

doc.xpath(\"//text() = \'Alliance Consulting\'\") do          


        
1条回答
  •  日久生厌
    2020-12-02 20:39

    This expression //text() = 'Alliance Consulting' evals to a boolean.

    In case of this test sample:

    
        Alliance Consulting
        
            

    Test string Alliance Consulting

    Alliance Consulting Other string

    It will return true of course.

    Expression you need should evaluate to node-set, so use:

    //text()[. = 'Alliance Consulting']
    

    E.g. expression:

    count(//text()[normalize-space() = 'Alliance Consulting'])
    

    against the above document will return 3.

    To select text nodes which contain 'Alliance Consulting' in the whole string value (e.g. 'Alliance Consulting provides great services') use:

    //text()[contains(.,'Alliance Consulting')]
    

    Do note that adjacent text nodes should become one after parser gets to the document.

    0 讨论(0)
提交回复
热议问题