1

希望从原始数据中“重建”图书节点。


如果这是正确的术语,标题数据如何与相应的年份数据合并?

假设标题和年份是用 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>

但是,作为单个元素返回。也可以看看:

Xquery 中的嵌套循环会导致不匹配?和成语

4

1 回答 1

1

不确定,但你在寻找这样的东西吗?

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>
于 2020-12-16T01:26:29.587 回答