Return multiple values ​​from multiple inputs in XQuery 3.0?

I have tried several things:

for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund') where $name contains text 'Hans' using fuzzy return $name | $name (: returns error: Stopped at line 3, column 20: [XPTY0004] Union expression: node() expected, xs:string found. :) for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund') where $name contains text 'Hans' using fuzzy return $name and $name (: returns true :) for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund') where $name contains text 'Hans' using fuzzy return $name, $name (: returns error: Stopped at line 3, column 19: [XPST0008] Undefined variable $name. :) 

What I'm trying to do is simply enter the name variable twice. Of course, I would like to do something more complex. For example, given a document (taken from w3):

 <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> 

I tried to fulfill some queries that I saw in the accepted answers via Stack Overflow, and I gave negative results. For instance:

 let $sep := ',' for $x in doc('test')/bookstore where fn:matches($x/book, 'XC') return fn:string-join( ($x/book/title/text(), $x/book/author/text(), $x/book/price/text()), $sep) (: Stopped at line 3, column 31: [XPTY0004] Single item expected, (element book { ... }, element book { ... }, ...) found. :) 

-

I would like to return the author, year and title of any book with a category attribute equal to "web", a title element whose language attribute is English, and whose value exactly matches "Learning XML" and whose any child contains the text "Eric T"; or whose year is later than 2003, whose descendant contains XML text whose price is less than $ 20.00.

I think this shows the versatility of XQuery. Without the ability to return and actually enter a list of goals, the software is practically useless. I found that there is no solid answer or guide that covers even a small part of the above request; however, there are questions about how to perform mathematical comparisons and order functions. They are useful in their own way, but at startup they are not as important as the ability to return multiple columns of a table based on the results found in it.

+6
source share
1 answer

In your third attempt, you were very close, only the brackets were missing:

 for $name in ('Hanz', 'Heinz', 'Hans', 'Huns', 'Hund') where $name contains text 'Hans' using fuzzy return ($name, $name) 

You can return arbitrary variables in this sequence.

Caution: XQuery does not know nested sequences, if you try to do this, they just concatenate. If you need nested sequences, you will need to build an XML tree that you will return, for example.

 return <names> <name>{$name}</name> <name>{$name}</name> </names> 

This one does not contain any sequences, but should show a pattern.

+13
source

Source: https://habr.com/ru/post/1434545/


All Articles