Out of Box Support for Extract all namespaces and attributes of an XML element

Is there any out of box way to extract all namespaces and attributes defined on an XML Node. Example <root>?

<root xmlns:ex1="http://example1.com" xmlns:ex2="http://example2.com" testAttr="1.2" xmlns="http://rooot.com">

<ex2:child></ex2:child>

</root>

I do not want to use Java Callout Policy.

I have tried using an Extract Variable Policy to extract the root node. I could use a JavaScript Policy to extract the namespaces and attributes but was hoping if there is a better way of achieving this.

Thanks in advance

0 6 294
6 REPLIES 6

Is there any out of box way to extract all namespaces and attributes defined on an XML Node.

No. There could be 1001 namespace declarations and 1763 attributes on a single XML Element. There's no "built in" way to extract all of that information into a set of variables.

What do you really want to accomplish? Why do you need all that metadata? What will you do with it?

I need to declare all namespaces globally . I have to transform the incoming XML payload into another XML with a root element and transfer all the namespaces to the newly added root element (I plan to use XSLT Policy unless there is a better way) . This example is what I need to achieve:


<ex3:newroot xmlns:ex3="http://example3.com" http://example1.com" xmlns:ex2=" http://example2.com" xmlns=" http://rooot.com">

<root testAttr="1.2">

...

<ex2:child></ex2:child>

</root>

</ex3:newroot>

Thanks

That's an interesting technical requirement. WHY do you need to do that?

You can do that with this XSL.

https://github.com/wendellpiez/XMLNamespaceFixup/blob/master/XSLT/namespace-cleanup.xsl

It is a limitation currently of another system where our data flows. They cannot process the entire message correctly unless the namespaces are declared globally and have the <newroot> as the root element. The limitation is not because of technical expertise but because of the limitation of the tools they use.
Thank you for the quick turnaround with solution. Looks like the XSLT traverses through entire document and parses every namespace and selectively eliminates duplicates . I may come up with similar XSLT but a more condensed version as I can make some assumptions as to how the payload will look like.

yup, that would work. That's how I would do what you're aiming for: with XSLT.

This is where XSLT can work its magic.

See https://stackoverflow.com/questions/38150145/get-namespaces-of-a-node-with-xslt/38150581 for an example on how to extract all namespaces

And http://www.java2s.com/Code/XML/XSLT-stylesheet/Listtheattributenamesandvalues.htm for an example on how to extract attributes (and their values)

You'd need to adapt these example to achieve your goal. You could for instance generate different XML elements for each attribute and namespace and take it from there.