tag0 namespace added for elements in default namespace tag0 namespace added for elements in default namespace xml xml

tag0 namespace added for elements in default namespace


You can construct the XmlSlurper with no namespace awareness like so:

import groovy.xml.XmlUtildef pom = new XmlSlurper( false, false ).parse( 'pom.xml' )println XmlUtil.serialize(pom)

Which should give you the answer you want... No idea currently about how to maintain comments during the slurp/serialize cycle :-(

As you say, it might be possible with XmlParser, but my current attempts have failed :-( There's some code here which might get you close, but as yet I've had no success :-(


I had the same issue with "tag0" getting added to elements that didn't define a namespace (i.e they were in the "no namespace" namespace). I fixed this by adding

declareNamespace('': '')

which resets elements from being in the default namespace to being in the "no namespace" namespace.


I found that it is better to use XmlParser rather than XmlSlurper if you are dealing with namespaces and having the tag0 problem. Syntactically they seem the same, eg:

def root = new XmlParser().parse(new File('example.xml'))println XmlUtil.serialize(root)

The above code would output the example.xml exactly as it should be including namespaces.

If you want to process the root in some way, eg find a specific node, use the Groovy API and output the result, eg

def root = new XmlParser().parse(new File('example.xml')def result = root."ns:Element"[0]println XmlUtil.serialize(result)