简体   繁体   English

如何使用 FLWOR 查询合并 XML 文件中元素的相应数据?

[英]How to merge corresponding data from elements in an XML file using a FLWOR query?

Looking to "rebuild" a book node from the original data.希望从原始数据中“重建”书籍节点。


How is the title data merged, if that's the correct terminology, with the corresponding year data?如果这是正确的术语,标题数据如何与相应的年份数据合并?

Assuming that the title and year are defined with the let operator.假设标题和年份是用 let 运算符定义的。

output: output:

<b>
  <t>Everyday Italian Harry Potter XQuery Kick Start Learning XML</t>
  <y>2005 2005 2003 2003</y>
</b>

query:询问:

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>

data:数据:

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

Also tried wrapping the entire query with an element node and curly braces.还尝试使用元素节点和花括号来包装整个查询。


output: output:

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

query:询问:

xquery version "3.1";

<b>
{

for $doc in db:open("bookstore")

return ($doc/bookstore/book/title,$doc/bookstore/book/year)

}
</b>

but, returns as a single element.但是,作为单个元素返回。 see also:也可以看看:

nested looping within Xquery results in a mismatch? Xquery 中的嵌套循环会导致不匹配? and idioms和成语

Not sure, but are you looking for something like this?不确定,但你在寻找这样的东西吗?

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

Output: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM