Update Xlink Element?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Update Xlink Element?

danprice303
Howdy.  I need to update the "xlink:title" attribute in:

<gmi:instrument xlink:title="$INSTRUMENT"/>

Tutorials on Codehaus demonstrate something similar:

// modify supplies: we need extra pens
def pens = root.category.find{ it.@type == 'supplies' }.item.findAll{ it.text() == 'Pens' }
pens.each { p ->
?????? p.@quantity = (p.@quantity.toInteger() + 2).toString()
?????? p.@when = 'Urgent'
}

But I can't seem to apply this to my "xlink:title" attribute--always errors with namespace declarations, unexpected chars (:), etc.  I could do a brute force find and replace, but I'd prefer to use an XmlSlurper method.  I'd much appreciate any advice, thanks.

D
Reply | Threaded
Open this post in threaded view
|

RE: Update Xlink Element?

Bob Brown
Have a play with this:

///
import groovy.xml.*

def xmlSource = '''
<bob:root
  xmlns:bob="stuff"
  xmlns:gmi="http://www.isotc211.org/2005/gmi"
  xmlns:xlink="http://www.w3.org/1999/xlink">
  <gmi:instrument xlink:title="$INSTRUMENT"/>
</bob:root>
'''

def bobRoot = new XmlSlurper(false,
true).parseText(xmlSource).declareNamespace(bob: 'stuff', ns2:
'http://www.example.org/NS2', gmi: "http://www.isotc211.org/2005/gmi")

def instrument = bobRoot.'gmi:instrument'

// NB: does NOT work: instrument[0].attributes().remove('xlink:title')
//instrument[0].attributes().remove('{http://www.w3.org/1999/xlink}title')

// NB: leads to duplicate attribute: instrument.'@xlink:title' = 'XXX'
instrument.'@{http://www.w3.org/1999/xlink}title' = 'XXX'

println new StreamingMarkupBuilder().bind {
  mkp.yield instrument
}.toString()

'OK'
///

The key here is finding out that you ned to use the full namespaced name,
not the shorter name.

IMHO, this is a bug and I have created
http://jira.codehaus.org/browse/GROOVY-6356 using this code.

I'm also wondering why I needed to use 'instrument[0]' to get at the
attributes. Also seems 'odd' to me.

HTH

BOB

> -----Original Message-----
> From: danprice303 [mailto:[hidden email]]
> Sent: Thursday, 19 September 2013 3:20 AM
> To: [hidden email]
> Subject: [groovy-user] Update Xlink Element?
>
> Howdy.  I need to update the "xlink:title" attribute in:
>
> <gmi:instrument xlink:title="$INSTRUMENT"/>
>
> Tutorials on Codehaus demonstrate something similar:
>
> // modify supplies: we need extra pens
> def pens = root.category.find{ it.@type == 'supplies' }.item.findAll{
> it.text() == 'Pens' }
> pens.each { p ->
> ?????? p.@quantity = ([hidden email]() + 2).toString() ??????
> p.@when = 'Urgent'
> }
>
> But I can't seem to apply this to my "xlink:title" attribute--always
errors with
> namespace declarations, unexpected chars (:), etc.  I could do a brute
force

> find and replace, but I'd prefer to use an XmlSlurper method.  I'd much
> appreciate any advice, thanks.
>
> D
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Update-
> Xlink-Element-tp5716939.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email