XPath : finding an attribute node (and only one)

后端 未结 3 737
终归单人心
终归单人心 2021-01-18 21:25

What is the XPath to find only ONE node (whichever) having a certain attribute (actually I\'m interested in the attribute, not the node). For example, in my XML, I have seve

相关标签:
3条回答
  • 2021-01-18 22:17

    Did you tried this?

    //@lang[1]
    

    here you can see an example.

    0 讨论(0)
  • 2021-01-18 22:24

    The following XPath seems to do what you want:

    //*[@lang][1]/attribute::lang
    
    0 讨论(0)
  • 2021-01-18 22:30

    After some more tackling, here is a working solution :

     (//@lang)[1]
    

    Parentheses are needed to separate the [1] from the attribute name, otherwise the position() function is applied within the parent element of the attribute (which is useless since there can be only one attribute of a certain name within a tag : that's why //@lang[2] always selects nothing).

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