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

Wrong value returned for "Attribute.name()" when iterating over GPath Attributes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.1
    • 1.7.2, 1.6.9, 1.8-beta-1
    • None
    • None
    • Mac OS X 10.6.2
    • Patch

    Description

      When iterating over a collection of GPath Attributes I'm seeing the wrong value being returned for the "Attribute.name()" method. The issue seems to be in the implementation of the "groovy.util.slurpersupport.Attributes.iterator()" method when constructing a new Attribute. It uses the "attributeName" field which holds the name of the attribute minus the '@' character. The "Attribute.name()" method is expecting the first character in the name to be '@' as well and throws it away. This leads to the first character in the attribute name going missing.

      I've attached a patch which shows one possible fix to the issue in Attributes.java. It also modifies GpathSyntaxTestSupport.groovy to add some Unit Tests for this issue.

      The following code demonstrates the problem I'm seeing.

      def xml = '''
      <people>
        <person age="20">John</person>
        <person age="25">Jane</person>
      </people>
      '''
      
      def p = new XmlSlurper().parseText(xml)
      p.person.each {
        println it.@age.name()
      }
      p.person.@age.each {
        println it.name()
      }
      

      Running the above gives the following output:
      age
      age
      ge
      ge

      Attachments

        1. patch.txt
          2 kB
          Michael Frayer

        Activity

          People

            paulk Paul King
            frayerm Michael Frayer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: