Using XPath, How do I select a node based on its text content and value of an attribute?

前端 未结 2 1219
春和景丽
春和景丽 2020-11-29 04:41

Given this XML:



    
        
            July
            
                   


        
相关标签:
2条回答
  • 2020-11-29 05:37

    Generally I would consider the use of an unprefixed // as a bad smell in an XPath.

    Try this:-

    /DocText/WithQuads/Page/Word[text()='July' and Quad/P1/@X > 90]
    

    Your problem is that you use the //P1[@X < 90] which starts back at the beginning of the document and starts hunting any P1 hence it will always be true. Similarly //P1[@X > 90] is always true.

    0 讨论(0)
  • 2020-11-29 05:47

    Apart form the "//" issue, this XML is a very weird use of mixed content. The predicate text()='July' will match the element if any child text node is exactly equal to July, which isn't true in your example because of surrounding whitespace. Depending on the exact definition of the source XML, I would go for [text()[normalize-space(.)='July'] and Quad/P1/@X > 90]

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