Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate between XML breadthFirst() traversal and * navigation #517

Closed
wants to merge 1 commit into from

Conversation

manouti
Copy link
Contributor

@manouti manouti commented Mar 23, 2017

The analogy between '*' and breadthFirst() traversal seems wrong. The former only traverses one level, while the latter searches through the next levels as well. See http://stackoverflow.com/questions/42985716/groovy-xml-tree-traversal-breadthfirst-method-using-as-syntactic-sugar:

def books = '''\
<response>
   <books>
      <book available="20" id="1">
         <title>foo</title>
         <author id="1">foo author</author>
      </book>
      <book available="14" id="2">
         <title>bar</title>
         <author id="2">bar author</author>
      </book>
   </books>
</response>'''

def response = new XmlSlurper().parseText(books)
def bk = response.'*'.find { node ->
   node.name() == 'book' && node['@id'].toInteger() == 2
}
assert bk.empty

whereas using breadthFirst() explicitly does what I expect both to do which is to do breadth-first traversal:

def books = '''\
<response>
   <books>
      <book available="20" id="1">
         <title>foo</title>
         <author id="1">foo author</author>
      </book>
      <book available="14" id="2">
         <title>bar</title>
         <author id="2">bar author</author>
      </book>
   </books>
</response>'''

def response = new XmlSlurper().parseText(books)
def bk = response.breadthFirst().find { node ->
   node.name() == 'book' && node['@id'].toInteger() == 2
}
assert bk.title == 'bar' // bk is no longer an empty list of children

The analogy between '*' and breadthFirst() traversal seems wrong. The former only traverses one level, while the latter searches through the next levels as well. See http://stackoverflow.com/questions/42985716/groovy-xml-tree-traversal-breadthfirst-method-using-as-syntactic-sugar.
@paulk-asert
Copy link
Contributor

The shortcut for depthFirst() is '**' while the shortcut for children() is '*'. Obviously what is there is not quite right. I'll check shortly whether I think your changes capture everything that we need.

@asfgit asfgit closed this in 3b77da3 Apr 22, 2017
@paulk-asert
Copy link
Contributor

I merged your changes with minor tweaks. Thanks, much appreciated!

asfgit pushed a commit that referenced this pull request Apr 22, 2017
asfgit pushed a commit that referenced this pull request Apr 22, 2017
asfgit pushed a commit that referenced this pull request Apr 22, 2017
asfgit pushed a commit that referenced this pull request May 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants