I have a XML document which contains an element with two attributes with the same name, but belonging to different namespaces:
If I configure an XMLSlurper to be namespace aware and supplies a map with the two namespaces ("":"http://somenamespace" and "":"http://someothernamespace").
When I later try to access the contents of ItemId that belongs to the ns2 namespace using the following expression:
I always get the result "Number1".
What am I doing wrong?
Thanks in advance!
Answering my own question:
It seems like XmlSlurper and GPathResults is not able to handle the case described above, instead I turned to XmlParser.
Here is a complete example program showing the error as well as the solution:
I have been looking further into this and found that XmlSlurper disregards namespaces when storing attributes of an element, using only the local name of the attribute.
This will cause an attribute with the same name as a previous attribute to overwrite the earlier attribute (with the same name).
I have also devised a fix for this which I am going to submit to the Groovy developers for examination.
To me, this looks like a bug.
Just one thing: XML attribute names can't be in the default namespace, unlike element names, which can. I see you have a default namespace in your examples. That might be part of the problem for the Groovy implementation but nevertheless the output you're seeing definitely looks wrong to me.
Came across this page trying to overcome the same problem today. I have complex xml from which I need to parse values. There are twelve namespaces. What is the best practice today ten years after this post to do this in a groovy script? Can anyone offer an example?
Thanks in advance for any help.