Change XML tag name
问题 I want to transform an XML document which I have parsed with XmlSlurper . The (identical) XML tag names should be replaced with the value of the id attribute; all other attributes should be dropped. Starting from this code: def xml = """<tag id="root"> | <tag id="foo" other="blah" more="meh"> | <tag id="bar" other="huh"/> | </tag> |</tag>""".stripMargin() def root = new XmlSlurper().parseText(xml) // Some magic here. println groovy.xml.XmlUtil.serialize(root) I want to get the following: