aspose file tools*
The moose likes Groovy and the fly likes XmlSlurper and Namespaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "XmlSlurper and Namespaces" Watch "XmlSlurper and Namespaces" New topic
Author

XmlSlurper and Namespaces

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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!


My free books and tutorials: http://www.slideshare.net/krizsan
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
Well done sorting it out, and giving the solution
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 04, 2006
Posts: 2198
    
    1
Filed a bugreport:
http://jira.codehaus.org/browse/GROOVY-4637
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XmlSlurper and Namespaces