Find all namespace declarations in an XML document - xPath 1.0 vs xPath 2.0

后端 未结 4 2067
渐次进展
渐次进展 2021-02-03 14:34

As part of a Java 6 application, I want to find all namespace declarations in an XML document, including any duplicates.

Edit: Per Martin\'s request, h

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 15:27

    As the previous thread indicates, //namespace::* will return all the namespace nodes, of which there are 16, according to both the XPath 1.0 and XPath 2.0 implementations. It doesn't surprise me if you've found an implementation that doesn't implement the spec correctly.

    Finding all the namespace declarations (as distinct from namespace nodes) is not in general possible with either XPath 1.0 or XPath 2.0, because the following two documents are considered equivalent at the data model level:

    document A:

    
      
     
    

    document B:

    
      
    
    

    But if we define a "significant namespace declaration" to be a namespace that is present on a child element but not on its parent, then you could try this XPath 2.0 expression:

    for $e in //* return
      for $n in $e/namespace::* return
         if (not(some $p in $n/../namespace::* satisfies ($p/name() eq $e/name() and string($p) eq string($n)))) then concat($e/name(), '->', $n/name(), '=', string($n)) else ()
    

提交回复
热议问题