如何使用 Xquery 用 XML 节点包装每个 return 结果?
How to wrap each return result with an XML node using Xquery?
也许可以在每个 return 周围添加一个额外的 for 循环,然后用一个节点包装结果?
修改 w3 学校样本 query:
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>
也许可以在每个 return 周围添加一个额外的 for 循环,然后用一个节点包装结果?
修改 w3 学校样本 query:
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>