xml 从 XQuery 3.0 中的多个输入返回多个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12454481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:41:03  来源:igfitidea点击:

Return multiple values from multiple inputs in XQuery 3.0?

xmlxqueryxquery-3.0

提问by Wolfpack'08

I've tried a few 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 just return the name variable twice. Of course, I'd like to do something more complicated. For example, given a doc (taken from w3):

我想要做的只是返回 name 变量两次。当然,我想做一些更复杂的事情。例如,给定一个文档(取自 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've tried to execute some queries I've seen in accepted answers across Stack Overflow, and I've yielded negative results. For example:

我试图执行一些我在 Stack Overflow 接受的答案中看到的查询,但我得到了否定的结果。例如:

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'd like to return the author, year, and title of any book with the category attribute equal to "web", a title element whose language attribute is English and whose value is exactly "Learining XML" and whose any child contain the text "Erik T"; or whose year is later than 2003, whose any descendant contain the text XML, and whose price is less than 20.00USD.

我想返回类别属性等于“web”的任何书籍的作者、年份和标题,标题元素的语言属性是英语,其值正好是“学习 XML”,其任何子元素都包含文本“埃里克·T”;或年份晚于 2003 年,其任何后代都包含文本 XML,且价格低于 20.00USD。

I think this shows the versatility of XQuery. Without being able to return, and in fact input, a list of targets, the software is essentially useless. I've found there is not a solid answer or tutorial that covers even a small portion of the above query; however, there are queries on how to do mathematical comparisons and order-by functions. These are useful in their own way, but getting started, they aren't nearly as important as being able to return multiple columns of a table based on the results found therein.

我认为这显示了 XQuery 的多功能性。由于无法返回,实际上是输入目标列表,该软件基本上是无用的。我发现没有一个可靠的答案或教程可以涵盖上述查询的一小部分;但是,有关于如何进行数学比较和排序函数的查询。这些以它们自己的方式很有用,但是开始时,它们并不像能够根据其中找到的结果返回表的多个列那么重要。

回答by Jens Erat

You've been really close in your third attempt, just the parentheses have been 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.

您可以在此序列中返回任意变量。

Caveat: XQuery does not know nested sequences, if you try so, they will just get concatenated. If you need nested sequences, you will have to construct an XML tree which you will return, eg.

警告:XQuery 不知道嵌套序列,如果您尝试这样做,它们只会被连接起来。如果您需要嵌套序列,则必须构造一个将返回的 XML 树,例如。

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

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

这个不包含任何序列,但它应该显示模式。