I have an XML document that contains attributes with qualified names. I want to get the attribute value using XmlSlurper, but trying to access the attribute without specifying a namespace does not work (below is a minimal example).
def rootNode = new XmlSlurper().parseText( '''<root xmlns:ex="http://example.com"> <one ex:a1="uno!"/> <ex:two>Some text!</ex:two> </root>''' ) assert rootNode.one[0] .@a1.text () == 'uno!'
rootNode.one[0] .@a1.text () will result in an empty string. If we use rootNode.one[0].'@ex:a1'.text() , we get the correct value, but it depends on the namespace prefix used in the document - and you cannot rely on it to be the same for other documents, which is important for the corresponding namespace.
So the question is: How can XmlSlurper be used to access the attribute value of an attribute that has an associated namespace without specifying a namespace prefix? (it would be nice if it needed a full namespace)
source share