问题描述
我正在使用 w3schools 的 书店 XML:
I'm playing with the bookstore XML from w3schools:
<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" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
我查询使用 XQuery 3.1:
which I query of using XQuery 3.1:
xquery version "3.1";
declare option output:method 'xml';
for $doc in db:open("bookstore")
let $books := $doc/bookstore/book
return(
for $book in $books
let $authors := $book/author
let $title := data($book/title)
return
<b>{
(<t>{$title}</t>,$authors)
}</b>
)
就目前而言,输出就是想要的结果.
The output, so far as it goes, is the desired result.
查询的嵌套 for 循环是可以理解的,但可能不是Xquery"-ish?
the nested for loop of the query is comprehensible, but perhaps not "Xquery"-ish?
至少在这个例子中,一本书有一个标题,但有多位作者,这可能会造成一些不匹配,因为这里的循环被使用或误用了.
That a book has, at least for this example, a single title and yet multiple authors, creates, perhaps a bit of a mismatch insofar as the loop here is being used or misused.
输出:
<b>
<t>Everyday Italian</t>
<author>Giada De Laurentiis</author>
</b>
<b>
<t>Harry Potter</t>
<author>J K. Rowling</author>
</b>
<b>
<t>XQuery Kick Start</t>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
</b>
<b>
<t>Learning XML</t>
<author>Erik T. Ray</author>
</b>
这个问题本身并不意味着代码审查,而是更多地寻找替代方法或更标准的方法.与上下文并非完全无关的切线:
This question is not meant to be a code-review per se, more looking for alternate or more standard approaches. A not entirely unrelated tangent for context:
https://martinfowler.com/bliki/OrmHate.html
推荐答案
老实说,我不确定您的问题是什么,但是您的 XQuery 可以简化很多
I'm not sure what your question is, to be honest, but your XQuery can be simplified quite a bit
for $book in db:open("bookstore")/bookstore/book
return <b>
<t>{string($book/title)}</t>
{$book/author}
</b>
这会导致创建相同的节点
which results in the same nodes being created
<b>
<t>Everyday Italian</t>
<author>Giada De Laurentiis</author>
</b>
<b>
<t>Harry Potter</t>
<author>J K. Rowling</author>
</b>
<b>
<t>XQuery Kick Start</t>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
</b>
<b>
<t>Learning XML</t>
<author>Erik T. Ray</author>
</b>
这篇关于Xquery 中的嵌套循环会导致不匹配吗?和习语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!