This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XmlSlurper and Namespaces

 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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!
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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:

There output from the above is:

Attribute value: attributeData1
Attribute value: attributeData2

Best wishes!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out, and giving the solution
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a bug to me.

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.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filed a bugreport:
http://jira.codehaus.org/browse/GROOVY-4637
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic