Can XPath match on parts of an element's name?

后端 未结 3 752

I want to do this:

//*fu

which returns all nodes whose name ends in fu, such as

相关标签:
3条回答
  • 2020-12-28 13:34

    Do something like:

    //*[ends-with(name(), 'fu')]
    

    For a good XPath reference, check out W3Schools.

    0 讨论(0)
  • 2020-12-28 13:35

    This answer is for XPath 1.0 where there is no equivalent of the XPath 2.0 standard ends-with() function.

    The following XPath 1.0 expression selects all elements in the xml document, whose names end with the string "fu":

    //*[substring(name(),string-length(name())-1) = 'fu']
    
    0 讨论(0)
  • 2020-12-28 13:52

    I struggled with Dimitre Novatchev's answer, it wouldn't return matches. I knew your XPath must have a section telling that "fu" has length 2.

    It's advised to have a string-length('fu') to determine what to substring.

    For those who aren't able to get results with his answer and they require solution with xpath 1.0:

    //*[substring(name(), string-length(name()) - string-length('fu') +1) = 'fu']

    Finds matches of elements ending with "fu"

    or

    //*[substring(name(), string-length(name()) - string-length('Position') +1) = 'Position']

    Finds matches to elements ending with "Position"

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