You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2016/04/09 21:49:56 UTC

groovy git commit: GROOVY-7781 - Cannot retrieve XML attribute with namespace (closes #302)

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X ec5aec992 -> c64d3ee43


GROOVY-7781 - Cannot retrieve XML attribute with namespace (closes #302)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c64d3ee4
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c64d3ee4
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c64d3ee4

Branch: refs/heads/GROOVY_2_4_X
Commit: c64d3ee43d1045ddd74a6ea58d6edb8638acb4d2
Parents: ec5aec9
Author: John Wagenleitner <jw...@apache.org>
Authored: Sat Apr 9 10:21:03 2016 -0700
Committer: John Wagenleitner <jw...@apache.org>
Committed: Sat Apr 9 12:47:59 2016 -0700

----------------------------------------------------------------------
 .../java/groovy/util/slurpersupport/Attribute.java |  2 +-
 .../groovy/util/slurpersupport/GPathResult.java    |  4 ++++
 .../test/groovy/groovy/util/XmlSlurperTest.groovy  | 17 +++++++++++++++--
 3 files changed, 20 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/c64d3ee4/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/Attribute.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/Attribute.java b/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/Attribute.java
index ae0104b..d5c5b4b 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/Attribute.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/Attribute.java
@@ -75,7 +75,7 @@ public class Attribute extends GPathResult {
      * @return the namespace of this Attribute
      */
     public String namespaceURI() {
-        if (namespacePrefix == null) return "";
+        if (namespacePrefix == null || namespacePrefix.isEmpty()) return "";
         String uri = namespaceTagHints.get(namespacePrefix);
         return uri == null ? "" : uri;
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/c64d3ee4/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java b/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
index 4bebe67..5aca4b6 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/util/slurpersupport/GPathResult.java
@@ -260,6 +260,10 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
      * @return the namespace of the prefix
      */
     public String lookupNamespace(final String prefix) {
+        Object namespace = namespaceMap.get(prefix);
+        if (namespace != null) {
+            return namespace.toString();
+        }
         return this.namespaceTagHints.isEmpty() ? prefix : this.namespaceTagHints.get(prefix);
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/c64d3ee4/subprojects/groovy-xml/src/test/groovy/groovy/util/XmlSlurperTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/util/XmlSlurperTest.groovy b/subprojects/groovy-xml/src/test/groovy/groovy/util/XmlSlurperTest.groovy
index c09231b..32a905b 100644
--- a/subprojects/groovy-xml/src/test/groovy/groovy/util/XmlSlurperTest.groovy
+++ b/subprojects/groovy-xml/src/test/groovy/groovy/util/XmlSlurperTest.groovy
@@ -138,9 +138,9 @@ class XmlSlurperTest extends GroovyTestCase {
             <ChildElement ItemId="FirstItemId" two:ItemId="SecondItemId">Child element data</ChildElement>
         </RootElement>"""
         def root = new XmlSlurper().parseText(xml).declareNamespace(one:"http://www.ivan.com/ns1", two: "http://www.ivan.com/ns2")
+        // Default namespace declarations do not apply to attribute names, see https://www.w3.org/TR/xml-names/#defaulting
         assert root.ChildElement.@ItemId == 'FirstItemId'
-        assert root.ChildElement.@ItemId[0].namespaceURI() == 'http://www.ivan.com/ns1'
-        assert root.ChildElement.@'one:ItemId' == 'FirstItemId'
+        assert root.ChildElement.@ItemId[0].namespaceURI() == ''
         assert root.ChildElement.@'two:ItemId' == 'SecondItemId'
         assert root.ChildElement.@'two:ItemId'[0].namespaceURI() == 'http://www.ivan.com/ns2'
     }
@@ -215,4 +215,17 @@ class XmlSlurperTest extends GroovyTestCase {
         // execute a DGM on the Iterable
         assert root.first() == 'Child element data'
     }
+
+    // GROOVY-7781
+    void testNamespacedAttributesAccessedWithDifferentPrefix() {
+        def xml = '''
+        <x:root xmlns:x="blah">
+            <x:child x:id="1">c</x:child>
+        </x:root>
+        '''
+
+        def root = new XmlSlurper(false, true).parseText(xml).declareNamespace(t: 'blah')
+        assert root.'t:child'.text() == 'c'
+        assert root.'t:child'.@'t:id' == '1'
+    }
 }