0
votes

I have the following xml

<group xmlns="http://www.cdisc.org/ns/odm/v1.3">
    <item>
        <text xml:lang="en">Mild</text>
        <text xml:lang="fr">Legere</text>
    </item>
<group>

I want to get all the "en" string from the xml. I am parsing it with the following groovy code

def doc = new XmlSlurper().parse(inputstream).declareNamespace(xmlns:'http://www.cdisc.org/ns/odm/v1.3')

List<String> text = []
def s = doc.item.find{ it@":lang" = "en"}.each {
    text.add(it.text())
}

println text

The problem is it seams to be ignoring the attribute. I registered the default namespace, i've tried combinations of xml:lang, :lang, lang in the find closure but no joy.

Does any body know what I'm doing wrong.

Thanks

1

1 Answers

1
votes

The xml prefix always refers to the namespace http://www.w3.org/XML/1998/namespace - this is fixed in the namespaces specification and does not need to be declared in an XML document.

So try

def doc = new XmlSlurper().parse(inputstream).declareNamespace(
   xml:'http://www.w3.org/XML/1998/namespace')

and then xml:lang should work as you expect.