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

GPathResult: Cannot retrieve XML attribute with namespace

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.5, 2.4.6
    • 2.4.7
    • XML Processing
    • None

    Description

      When using GPathResult with XmlSlurper, if an attribute is in a namespace, you must retrieve it using the namespace prefix defined in the document and not that defined by declareNamespace. This issue prevents safe retrieval of namespaced XML attributes since the document generator is free to choose namespace prefix.

      def text = """<x:root xmlns:x="blah">
        <x:child x:id='1'>c</x:child>
      </x:root>"""
      
      def xml =
          new XmlSlurper() 
              .parseText(text)
              .declareNamespace(x:'blah')
      //        .declareNamespace(t:'blah')
      
      println xml.child.text()     //"c" always
      println xml.'x:child'.text() //"c" when declareNamespace x, else ""
      println xml.'t:child'.text() //"c" when declareNamespace t, else ""
      println xml.child.'@x:id'    //"1" always
      println xml.child.'@t:id'    //"" always
      

      Looking in the debugger, there is namespaceMap and namespaceTagHints in GPathResult. namespaceMap is updated by declareNamespace but namespaceTagHints is not, and namespaceTagHints is used when looking up attributes. I don't see a way to update namespaceTagHints and namespaceMap doesn't really even seem to be used.

      Attachments

        Issue Links

          Activity

            People

              jwagenleitner John Wagenleitner
              gillius Jason Winnebeck
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: