如何使用Xquery用XML节点包装每个返回结果



也许可以在每个返回周围添加一个额外的for循环,从而用节点包装结果?

修改w3学校示例查询:

nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ basex bookstore.category.name.xq 
<xml>
<category>cooking</category>
<title>Everyday Italian</title>
<category>children</category>
<title>Harry Potter</title>
<category>web</category>
<title>Learning XML</title>
<category>web</category>
<title>XQuery Kick Start</title>
</xml>nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ cat bookstore.category.name.xq 
xquery version "3.1";

<xml>
{
for $x in db:open("bookstore")/bookstore/book
order by $x/title
return (<category>{data($x/@category)}</category>,<title>{data($x/title)}</title>)
}
</xml>
nicholas@mordor:~/flwor$ 

如何用book元素包装每个结果?

xml样本数据:

<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>

来自w3schools网站。

应该这样做:

<xml>
{
for $x in db:open("bookstore")/bookstore/book
let $category := data($x/@category)
return (
for $book in $x  
let $title := data($book/title)
return(
<book>
<category>{$category}</category><title>{$title}</title>)
</book>)
)
}
</xml>

输出:

<xml>
<book>
<category>cooking</category>
<title>Everyday Italian</title>)
</book>
<book>
<category>children</category>
<title>Harry Potter</title>)
</book>
<book>
<category>web</category>
<title>XQuery Kick Start</title>)
</book>
<book>
<category>web</category>
<title>Learning XML</title>)
</book>
</xml>

最新更新