【问题标题】:Is it possible to use OR on a where clause in XQuery?是否可以在 XQuery 中的 where 子句上使用 OR?
【发布时间】: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


【解决方案1】:

抱歉,我错过了您查询的路径。只有一个库元素,因此您的 for 循环仅迭代一次,然后由于 至少一个 &lt;author&gt; 匹配您的 where 子句,它返回来自 &lt;library&gt; 的所有值。

解决方案是在层次结构中向下迭代一层:

for $x in doc("xmldata.xml")/library/*
where $x/author = 'John Doe' 
return $x/title

或者如果你想非常清楚你选择了哪些元素:

for $x in doc("xmldata.xml")/library/(article|book)
...

要控制不同元素的值输出,您可以在返回中使用不同的 XPath:

...
return ($x/self::book/title, $x/self::article/author)

【讨论】:

  • 您好,不确定我是否理解您的回复? :) 你是说我的查询有误吗?我想在 library/article/author = "John Doe" 或 library/book/author = "John Doe" 时返回标题
  • 抱歉我回复得太早了!它只是说“对不起,我错过了你查询的路径。”这很好用,谢谢!解释也很有帮助
  • 您好,抱歉,我还有一件事要做,即退回文章的作者和书籍的标题。如果我将作者添加到返回中,它仍然会返回标题。有什么办法吗?
  • 太棒了!谢谢,现在有道理,将加倍“自我”知识。我只是好奇的最后一件事 - 在 XQuery 中是否有可能拥有它,以便它返回作者是“John Doe”或“Jane Doe”的数据 - 使用 |似乎不起作用,或者
  • @user2633709 我不确定这是否是您要问的,但如果您使用$x/author = ('Author 1', 'Author 2') 之类的表达式,如果 any 项目中的序列匹配。
【解决方案2】:

您可以使用通配符 * 来匹配元素,无论其名称如何:

doc("xmldata.xml")/library/*[author = 'John Doe']/title

这相当于更冗长的 FLWOR 表达式

for $entry in doc("xmldata.xml")/library/*
where $entry/author = 'John Doe'
return $entry/title

【讨论】:

  • 您好,抱歉,我还有一件事要做,即退回文章的作者和书籍的标题。如果我将作者添加到返回中,它仍然会返回标题。有什么办法吗?
  • 有很多方法可以做到这一点;试试这个,例如:let $lib := doc("xmldata.xml")/library return ($lib/article[author = 'John Doe']/author, $lib/book[author = 'John Doe']/title)。如有其他问题,最好打开一个新的 StackOverflow 条目。
猜你喜欢
  • 2017-07-18
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
相关资源
最近更新 更多