Get nodes where child node contains an attribute

前端 未结 5 1165
长情又很酷
长情又很酷 2020-11-30 21:13

Suppose I have the following XML:


  Purgatorio
  Dante Alighieri<         


        
5条回答
  •  离开以前
    2020-11-30 21:29

    Try

    //book[title/@lang = 'it']
    

    This reads:

    • get all book elements
      • that have at least one title
        • which has an attribute lang
          • with a value of "it"

    You may find this helpful — it's an article entitled "XPath in Five Paragraphs" by Ronald Bourret.

    But in all honesty, //book[title[@lang='it']] and the above should be equivalent, unless your XPath engine has "issues." So it could be something in the code or sample XML that you're not showing us -- for example, your sample is an XML fragment. Could it be that the root element has a namespace, and you aren't counting for that in your query? And you only told us that it didn't work, but you didn't tell us what results you did get.

提交回复
热议问题