如何使用 FLWOR 查询从 XML 文件中的元素合并相应数据?
How to merge corresponding data from elements in an XML file using a FLWOR query?
希望从原始数据“重建”图书节点。
标题数据如何与相应的年份数据合并(如果这是正确的术语)?
假设标题和年份是用 let 运算符定义的。
输出:
<b>
<t>Everyday Italian Harry Potter XQuery Kick Start Learning XML</t>
<y>2005 2005 2003 2003</y>
</b>
查询:
xquery version "3.1";
for $doc in db:open("bookstore")
let $title := data($doc/bookstore/book/title)
let $year := data($doc/bookstore/book/year)
return
<b>
<t>{$title}</t>
<y>{$year}</y>
</b>
数据:
<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>
还尝试用元素节点和花括号包裹整个查询。
输出:
<b>
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
<year>2005</year>
<year>2005</year>
<year>2003</year>
<year>2003</year>
</b>
查询:
xquery version "3.1";
<b>
{
for $doc in db:open("bookstore")
return ($doc/bookstore/book/title,$doc/bookstore/book/year)
}
</b>
但是,returns 作为单个元素。
另见:
不确定,但您正在寻找这样的东西吗?
for $doc in db:open("bookstore")/bookstore/book
return (
for $book in $doc
let $title := data($book/title),
$year := data($book/year)
return(
<b>
<t>{$title}</t>
<y>{$year}</y>)
</b>)
)
输出:
<b>
<t>Everyday Italian</t>
<y>2005</y>)
</b>
<b>
<t>Harry Potter</t>
<y>2005</y>)
</b>
<b>
<t>XQuery Kick Start</t>
<y>2003</y>)
</b>
<b>
<t>Learning XML</t>
<y>2003</y>)
</b>
希望从原始数据“重建”图书节点。
标题数据如何与相应的年份数据合并(如果这是正确的术语)?
假设标题和年份是用 let 运算符定义的。
输出:
<b>
<t>Everyday Italian Harry Potter XQuery Kick Start Learning XML</t>
<y>2005 2005 2003 2003</y>
</b>
查询:
xquery version "3.1";
for $doc in db:open("bookstore")
let $title := data($doc/bookstore/book/title)
let $year := data($doc/bookstore/book/year)
return
<b>
<t>{$title}</t>
<y>{$year}</y>
</b>
数据:
<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>
还尝试用元素节点和花括号包裹整个查询。
输出:
<b>
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
<year>2005</year>
<year>2005</year>
<year>2003</year>
<year>2003</year>
</b>
查询:
xquery version "3.1";
<b>
{
for $doc in db:open("bookstore")
return ($doc/bookstore/book/title,$doc/bookstore/book/year)
}
</b>
但是,returns 作为单个元素。 另见:
不确定,但您正在寻找这样的东西吗?
for $doc in db:open("bookstore")/bookstore/book
return (
for $book in $doc
let $title := data($book/title),
$year := data($book/year)
return(
<b>
<t>{$title}</t>
<y>{$year}</y>)
</b>)
)
输出:
<b>
<t>Everyday Italian</t>
<y>2005</y>)
</b>
<b>
<t>Harry Potter</t>
<y>2005</y>)
</b>
<b>
<t>XQuery Kick Start</t>
<y>2003</y>)
</b>
<b>
<t>Learning XML</t>
<y>2003</y>)
</b>