groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <>
Subject RE: XmlSlurper, attributes and namespaces
Date Tue, 01 Mar 2016 20:21:59 GMT
I can at least give the technical reason why this doesn't work -- there is namespaceMap and
namespaceTagHints in GPathResult. namespaceMap is updated by declareNamespace but namespaceTagHints
is not. I don't see a way to update namespaceTagHints and namespaceMap doesn't really even
seem to be used. This seems like a bug in GPathResult?


-----Original Message-----
From: Winnebeck, Jason [] 
Sent: Tuesday, March 01, 2016 3:03 PM
Subject: XmlSlurper, attributes and namespaces

I've been struggling for a long time with XmlSlurper in how I can read attributes. Unfortunately,
I have an XML document with namespaces. I wish I could ignore the namespaces, but while XmlSlurper
no-arg constructor says it ignores namespaces, it does not. When using two-arg constructor
to set namespaceAware to false it just asks like namespaced elements are deleted. So I'm trying
to figure out how to specify namespaces:

import groovy.xml.*

def text = """<x:root xmlns:x="blah">
  <x:child x:id='1'>c</x:child>

def xml =
    new XmlSlurper() 
//        .declareNamespace(t:'blah')

println xml.child.text()     //"c" always
println xml.'x:child'.text() //"c" when declareNamespace x println xml.'t:child'.text() //"c"
when declareNamespace t
println xml.child.'@x:id'    //"1" always
println xml.child.'@t:id'    //"" always

It appears that specifying namespace is optional on elements and also declareNamespace affects
how I find the elements when they do have namespaces. For attributes, declareNamespace appears
to have no effect, and I need to specify the prefix as it is specified in the file itself.
The problem is that the generator gets to specify any prefix they want. How can I get the
"id" attribute on the "child" element regardless of the namespace prefix used? (A solution
dropping all namespaces is fine as there is only one namespace and no collisions).

Jason Winnebeck

This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.

View raw message