【发布时间】:2016-04-07 20:33:43
【问题描述】:
我需要返回“John Doe”为作者的书籍和期刊的标题,但我的 xml 文件设置为:
<library>
<book>...</book>
<article>...</article>
</library>
总共有 6 本书和期刊。
我知道如果这是 SQL,我可以这样做:
SELECT title
FROM library
WHERE bookauthor = "John Doe" OR articleauthor = "John Doe"
(该数据库不会被规范化,但我试图表明我认为我知道我需要做什么,只是不确定如何使用 XQuery)
我尝试了以下方法,它返回了所有 6 个标题:
for $x in doc("xmldata.xml")/library
let $a := $x/article
let $b := $x/book
return ($a/title, $b/title)
但我不确定如何处理 where 子句。同样,我尝试了以下操作并卡在同一点:
for $x in doc("xmldata.xml")/library
return ($x/article/title, $x/book/title)
当我尝试添加 where 子句时,它仍然返回所有 6 个条目,即使它应该只返回 1 本书和 1 篇文章:
for $x in doc("xmldata.xml")/library
where $x/article/author = 'John Doe'
where $x/book/author = 'John Doe'
return ($x/article/title, $x/book/title)
请问有人可以帮帮我吗?也许通过指出我正确的方向或指出我哪里出错了。
完整的 XML 文件:
<library>
<book>
<author>John Doe</author>
<title>Turnitin</title>
</book>
<article>
<author>John Doe</author>
<title>Evaluating</title>
</article>
<article>
<author>Shannon, L.</author>
<title>Reconceptualising</title>
</article>
<book>
<author>Burden, David</author>
<title>An evaluation</title>
</book>
<article>
<author>Moscrop, C.</author>
<title>Evaluating a systematic method</title>
</article>
<book>
<author>Beaumont, C.</author>
<title>Beyond e-learning</title>
</book>
</library>
【问题讨论】:
-
您能否发布更大的源 XML 示例?我看不出您上次的查询有什么问题。
-
我已经将源 XML 添加到帖子底部,谢谢!
标签: xml database xpath xquery basex