Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6356

Working with namespaced attributes awkward with XmlSlurper

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.7
    • Fix Version/s: 2.3.0-rc-1
    • Component/s: XML Processing
    • Labels:
      None
    • Environment:
      Win 8/64 java 1.7_40

      Description

      When trying to update (or remove) the xlink:title attribute in the following, one has to use the full namespaced tag name.

      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')
      // does work: 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 instrument.'@xlink:title'
      
      println new StreamingMarkupBuilder().bind {
        mkp.yield instrument
      }.toString()
      
      'OK'
      

      I SHOULD be able to simply do:

      instrument.'@xlink:title' = 'XXX'
      

      Note that I can do:

      println instrument.'@xlink:title'
      

      So there is an inconsistency/incompleteness/confusion here.

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

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              alpheratz alpheratz
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: