【问题标题】:How to use two conditions in "where" clause in XQuery如何在 XQuery 的“where”子句中使用两个条件
【发布时间】:2017-09-13 16:34:19
【问题描述】:

我正在尝试仅提取具有特定类型 <xref> 类型并使用 Xquery 匹配特定外部参照列表的 <book> 数据(我是新手)。

这是输入数据:

  <book id="6636551">
    <master_information>
        <book_xref>
            <xref type="Fiction" type_id="1">72771KAM3</xref>
            <xref type="Non_Fiction" type_id="2">US72771KAM36</xref>
        </book_xref>
    </master_information>
  </book>
  <book id="119818569">
    <master_information>
        <book_xref>
            <xref type="Fiction" type_id="1">070185UL5</xref>
            <xref type="Non_Fiction" type_id="2">US070185UL50</xref>
        </book_xref>
    </master_information>
  </book>
  <book id="119818568">
  <master_information>
        <book_xref>
            <xref type="Fiction" type_id="1">070185UK7</xref>
            <xref type="Non_Fiction" type_id="2">US070185UK77</xref>
        </book_xref>
    </master_information>
  </book>
  <book id="119818567">
    <master_information>
        <book_xref>
            <xref type="Fiction" type_id="1">070185UJ0</xref>
            <xref type="Non_Fiction" type_id="2">US070185UJ05</xref>
        </book_xref>
    </master_information>
  </book>
  <book id="38085123">
    <master_information>
        <book_xref>
            <xref type="Fiction" type_id="1">389646AV2</xref>
            <xref type="Non_Fiction" type_id="2">US389646AV26</xref>
        </book_xref>
    </master_information>
  </book>

我正在使用的 XQuery:

for $x in //book 
where $x//xref/@type='Fiction'
and
$x//xref=('070185UL5','070185UJ0')
return $x

上面的 Xquery 只获取匹配“070185UL5”的第一本书信息。我希望它能够同时获取两者。怎么了?感谢您的回复。

【问题讨论】:

  • 它确实获取了两者。
  • 对我来说看起来不错,并且在使用 Saxon 进行测试时可以正常工作。也许尝试将您的 where 子句更改为 where $x//xref[@type='Fiction'][normalize-space()=('070185UL5','070185UJ0')]
  • 对不起。当我现在再次尝试时,它确实获取了两者。我不知道为什么它以前不起作用。感谢您的宝贵时间!

标签: xml xpath xquery basex


【解决方案1】:

在查询中

for $x in //book 
where $x//xref/@type='Fiction'
and
$x//xref=('070185UL5','070185UJ0')
return $x

你是不是想说

(1) "必须至少有一个@type 为'Fiction' 的外部参照和至少一个值为'070185UL5' 或'070185UJ0' 的外部参照"

还是你想说

(2) "必须至少有一个外部参照,其@type 为'Fiction',值为'070185UL5' 或'070185UJ0'"

目前您说的是 (1)。如果你想说(2)那么查询应该是

for $x in //book 
where $x//xref[@type='Fiction' and .=('070185UL5','070185UJ0')]
return $x

您可以将其简化为 XPath 表达式

//book[.//xref[@type='Fiction' and .=('070185UL5','070185UJ0')]]

使用您提供的数据,两个查询给出相同的结果,但使用不同的数据,它们可能会给出不同的结果。

【讨论】:

  • 嗨迈克尔,感谢您的意见!我没有那样想。我打算说“只显示外部参照等于'070185UL5'或'070185UJ0'的'Fiction'类型”。虽然,我基本上明白你在说什么,但我不明白如何区分“至少”与“仅显示”的这种细微差别。如何在 XQuery 中强制执行这一点? (对不起,我是新手)。
  • 这并不是 XQuery 所特有的,它是基本的布尔逻辑。 “有上学的孩子和有残疾的孩子的父母”和“有上学的残疾孩子的父母”是有区别的。
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 2019-11-16
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多