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

GPathResult: Cannot retrieve XML attribute with namespace

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.5, 2.4.6
    • Fix Version/s: 2.4.7
    • Component/s: XML Processing
    • Labels:
      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.

        Issue Links

          Activity

          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user jwagenleitner opened a pull request:

          https://github.com/apache/groovy/pull/302

          GROOVY-7781 - Cannot retrieve XML attribute with namespace

          This change broke another test that asserted that an unprefixed attribute's namespace is the default namespace, but default namespace declarations do not apply to attribute names, see https://www.w3.org/TR/xml-names/#defaulting.

          > The namespace name for an unprefixed attribute name always has no value.

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/jwagenleitner/groovy GROOVY-7781

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/groovy/pull/302.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #302


          commit 7b652bb329bc64fcaa978fef0778fc720d3e9029
          Author: John Wagenleitner <jwagenleitner@apache.org>
          Date: 2016-03-23T22:15:54Z

          GROOVY-7781 - Cannot retrieve XML attribute with namespace


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user jwagenleitner opened a pull request: https://github.com/apache/groovy/pull/302 GROOVY-7781 - Cannot retrieve XML attribute with namespace This change broke another test that asserted that an unprefixed attribute's namespace is the default namespace, but default namespace declarations do not apply to attribute names, see https://www.w3.org/TR/xml-names/#defaulting . > The namespace name for an unprefixed attribute name always has no value. You can merge this pull request into a Git repository by running: $ git pull https://github.com/jwagenleitner/groovy GROOVY-7781 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/302.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #302 commit 7b652bb329bc64fcaa978fef0778fc720d3e9029 Author: John Wagenleitner <jwagenleitner@apache.org> Date: 2016-03-23T22:15:54Z GROOVY-7781 - Cannot retrieve XML attribute with namespace
          Hide
          gillius Jason Winnebeck added a comment -

          Not sure if GH comments get copied to here, but I did check out the pull request, and it looks like the changes I would expect and the unit test looks like it would properly reproduce the bug as well as pass when the fix is good.

          Show
          gillius Jason Winnebeck added a comment - Not sure if GH comments get copied to here, but I did check out the pull request, and it looks like the changes I would expect and the unit test looks like it would properly reproduce the bug as well as pass when the fix is good.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/groovy/pull/302

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/302
          Hide
          jwagenleitner John Wagenleitner added a comment -

          Thanks for reporting the issue and for reviewing and commenting on the proposed fix, really appreciate it.

          Show
          jwagenleitner John Wagenleitner added a comment - Thanks for reporting the issue and for reviewing and commenting on the proposed fix, really appreciate it.

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development