Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple namespaces in XPATH

There are 2 xml document with different namespaces, e.g.

declare namespace ml1 = "urn:schemas-com:ml1";
declare namespace ml2 = "urn:schemas-com:ml2";

but with same structure. Can I select some nodes using only these namespaces, without *? I mean something like that:

fn:doc($uri)/(ml1 or ml2):root

Updated: Thanks. All suggestions work but not useful for me. Because there can be 3 or more namespaces, and xpath expression will be very large. Any suggestions?

Conclusion: Thanks for the answers. I agree with @MichaelKay that I should process all xml all documents. Problem is solved.

like image 543
Andrei Orlov Avatar asked Mar 04 '26 06:03

Andrei Orlov


2 Answers

One possible expression is:

  doc($uri)/(ml1:root|ml2:root)

Another is:

  doc($uri)/*[self::ml1:root or self::ml2:root]

In case there can be an unlimited number of prefixes of the form "mlNNNNN"", use:

  doc($uri)/*[matches(substring-before(name(), ':'), '^ml\d+')]
like image 138
Dimitre Novatchev Avatar answered Mar 07 '26 03:03

Dimitre Novatchev


You can use predicates

fn:doc($uri)/*[local-name() = "root" and (namespace-uri() = "urn:schemas-com:ml1" or namespace-uri() = "urn:schemas-com:ml2")]

If your namespace URIs all have a common prefix (as the examples you've supplied suggest) then you can use a construction like

..../*[starts-with(namespace-uri(), "urn:schemas-com:") ...]

If you have a number of namespaces and you can list them all in another XML file:

<namespaces>
  <uri>urn:schemas-com:ml1</uri>
  <uri>urn:schemas-com:ml2</uri>
  <!-- ... -->
</namespaces>

and then the predicate

*[namespace-uri() = fn:doc("namespaces.xml")/namespaces/uri]

would be true if the namespace URI of the node being tested matches any of the namespaces listed in namespaces.xml.

like image 26
Ian Roberts Avatar answered Mar 07 '26 05:03

Ian Roberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!