[xquery-talk] momentary recursion dyslexia
Philippe Michiels
philippe.michiels at gmail.com
Wed Nov 30 13:58:23 PST 2005
Using the string-join function, you can even write down this query as a simple
path expression:
$book//fn:string-join(ancestor-or-self::*/name(), '/')
returns in Galax:
"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"
-Ph
On Sunday 27 November 2005 21:30, Howard Katz wrote:
> Peter Coppens has provided a solution. I'm assuming he doesn't mind if I
> post this below.
>
> I'd already figured out nearly identical solutions using a longer XQuery to
> do the traversals, but it's nice to be reminded that a terse XPath can work
> just as well as a lengthier FLWOR. This:
>
> concat( $p, '/'[ $p ], ... )
>
> is particularly sweet.
>
> Interestingly enough, this query works fine in Saxon but throws a type
> error ("Type of value does not match sequence type") in Galax and a
> "NOTANODE" error in Mark Logic. Boiled down, what these processors appear
> to be complaining about is this:
>
> <a/>/fn:name(.)
>
> Can someone tell me why this is problematic for these two engines?
> Howard
>
> > -----Original Message-----
> > From: Peter Coppens [mailto:Peter.Coppens at datadirect.com]
> > Sent: November 27, 2005 12:43 AM
> > To: Howard Katz
> > Subject: RE: [xquery-talk] momentary recursion dyslexia
> >
> > Here is a quick attempt. Not entirely what you want but
> > perhaps it helps
> >
> > declare function local:f1($p,$i) {
> > concat($p,'/'[$p],fn:name($i)),
> > $i/*/local:f1(concat($p,'/'[$p],fn:name($i)),.)
> > };
> >
> > declare function local:f2($p,$i) {
> > $i/*/local:f2(concat($p,'/'[$p],fn:name($i)),.),
> > concat($p,'/'[$p],fn:name($i))
> > };
> >
> > let $book :=
> > <book>
> > <author><name><last/><first/></name></author>
> > <author><name><last/><first/></name></author>/
> > </book>
> > return (
> > <f1>{local:f1("",$book)}</f1>,
> > <f2>{local:f2("",$book)}</f2>
> > )
> >
> > Hth,
> >
> > Peter
> >
> > > -----Original Message-----
> > > From: talk-bounces at xquery.com [mailto:talk-bounces at xquery.com] On
> >
> > Behalf
> >
> > > Of Howard Katz
> > > Sent: Friday, November 25, 2005 11:40 PM
> > > To: talk at xquery.com
> > > Subject: [xquery-talk] momentary recursion dyslexia
> > >
> > > 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
> > >
> > > _______________________________________________
> > > talk at xquery.com
> > > http://xquery.com/mailman/listinfo/talk
>
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk
More information about the talk
mailing list