[英]How to use two conditions in “where” clause in XQuery
我試圖僅提取具有特定類型的<xref>
類型的那些<book>
數據,並使用Xquery匹配特定外部參照的列表(我對此並不陌生)。
這是輸入數據:
<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”匹配的第一本書信息。 我希望它能同時獲取兩者。 怎么了? 感謝您的回復。
在查詢中
for $x in //book
where $x//xref/@type='Fiction'
and
$x//xref=('070185UL5','070185UJ0')
return $x
你打算說嗎
(1)“必須至少有一個@ref類型為'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')]]
使用您提供的數據,兩個查詢給出的結果相同,但是使用不同的數據,它們可以給出不同的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.