[xquery-talk] momentary recursion dyslexia

Howard Katz howardk at fatdog.com
Fri Nov 25 14:39:52 PST 2005


This seems like it should be a fairly straightforward CS101-style problem ,
but I can't seem to get it quite right. What I want is a recursive function
that traverse an xml tree and builds xpaths. Given this variable assignment:

   let $book := 
      <book>
          <author><name><last/><first/></name></author>
          <author><name><last/><first/></name></author>/
      </book>

I'd like to have the function walk the $book tree, printing as it goes:

book
book/author
book/author/name
book/author/name/last
book/author/name/first
book/author
book/author/name
book/author/name/last
book/author/name/first

To make things even more interesting, I'd also like (possibly via a
different function, or the same one parameterized), to be able to print out
the xpaths from the leaves up, as in:

book/author/name/last
book/author/name/first
book/author/name
book/author
book
book/author/name/last
book/author/name/first
book/author/name
book/author
book

I'm not sure if this reverse walker should be listing the root node twice or
not, but that's possibly a separate issue.

At any rate I'm having a momentary bout of recursion dyslexia. Can anyone
help?

TIA,
Howard



More information about the talk mailing list