How to find elements by attribute namespace in XPath

后端 未结 4 1994
春和景丽
春和景丽 2020-12-05 01:06

I\'m trying to use XPath to find all elements that have an element in a given namespace.

For example, in the following document I want to find the foo:bar

相关标签:
4条回答
  • 2020-12-05 01:12

    You could try

    //*[namespace-uri()='http://foo.example.com' or @*[namespace-uri()='http://foo.example.com']]
    

    It will give you element foo:bar and element doodah (if you change tal:quux to foo:quux in your XML-data):

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:foo="http://foo.example.com" xmlns:tal="xxx">
      <foo:bar quux="value">Content</foo:bar>
      <widget>Content</widget>
      <doodah foo:quux="value">Content</doodah>
    </root>
    

    Is that what you want?

    0 讨论(0)
  • 2020-12-05 01:17

    Your XPath expression is almost perfect. Instead of asking for attributes "@" ask for elements "" and it should work:

    "//*[namespace-uri()='http://foo.example.com']"
    
    0 讨论(0)
  • 2020-12-05 01:21

    Use:

    //*[namespace-uri()='yourNamespaceURI-here'
       or
        @*[namespace-uri()='yourNamespaceURI-here']
       ]
    

    the predicate two conditions are or-ed with the XPath or operator.

    The XPath expression thus selects any element that either:

    • belongs to the specified namespace.
    • has attributes that belong to the specified namespace.
    0 讨论(0)
  • 2020-12-05 01:24

    I'm not sure if this is what you mean, but by only deleting one char in your XPath you get all elements in a certain namespace:

    //*[namespace-uri()='http://foo.example.com']
    
    0 讨论(0)
提交回复
热议问题