XQuery:返回多个元素 [英] XQuery: Returning more than one element
问题描述
以下是描述我正在使用的 XML 结构的 DTD:
The following is the DTD describing the structure of the XML I am working with:
<!DOCTYPE bib [
<!ELEMENT bib (book+,magazine*)>
<!ELEMENT book (title,author+,publisher,editor?,price)>
<!ELEMENT magazine (title,publisher,editor+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (last,first)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT editor (last, first,affiliation)>
<!ELEMENT price ((#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT affiliation (#PCDATA)>
<!ATTLIST book year CDATA #REQUIRED>
]>
我必须编写一个 XQuery 来返回 Weikum 先生是其中一位编辑的所有书籍或杂志.
I have to write an XQuery to return all books or magazines where Mr. Weikum is one of the editors.
我在确定如何归还书籍或杂志时遇到问题.这是我能想到的:
I am having a problem in determining how to return book or magazine. This is what I can come up with:
for $x in document("bib.xml")/bib
where $x/book/editor/lastname = "Weikum" or $x/magazine/editor/lastname = "Weikum"
return <result></result>
但这并没有达到我想要的结果.如何退回书籍或杂志元素?我应该说 return $x
吗?
But this is not achieving my desired result. How can I return the book or magazine element? Should I just say return $x
?
推荐答案
返回姓氏为Weikum"的编辑的书籍和杂志的最简单方法是
The simplest way to return books and magazines with an editor whose last name is 'Weikum' would be
//editor[lastname='Weikum']/..
或者,如果您更喜欢 FLWOR 表达式,是的,您可以直接说 return $x
,如果 $x 绑定到您要返回的内容.但是,在您的草图中,情况并非如此:您说要归还书籍或杂志,但您已将 $x 绑定到 bib
元素.你想要更像这样的东西:
Or, if you prefer a FLWOR expression, yes, you can just say return $x
, if $x is bound to what you want to return. That's not true in your sketch, however: you say you want to return the book or magazine, but you have bound $x to the bib
element. You want something more like this:
for $x in document('bib.xml')/bib/*
where $x/editor/lastname='Weikum'
return $x
在这两个公式中,我利用了这样一个事实,即只有书籍和杂志才有名为 editor
的子项,并且只有书籍和杂志才会显示为 bib
的子项.在更复杂的 DTD 中,如果您需要过滤掉小册子、标准和博客文章以便只是书籍和杂志...
In these two formulations I am exploiting the fact that only books and magazines have children named editor
, and that only books and magazines appear as children of bib
. In a more complicated DTD, you might need more complex expressions if you needed to filter out pamphlets, standards, and blog posts in order to get just books and magazines ...
这篇关于XQuery:返回多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!