使用XQuery计算XML中唯一节点的数量。

问题描述 投票:0回答:1
<Bookstore>
<Book ISBN="ISBN-0-13-035300-0" Price="65" Edition="2nd">
<Title>A First Course in Database Systems</Title>
<Authors>
<Author>
<First_Name>Jeffrey</First_Name>
<Last_Name>Ullman</Last_Name>
</Author>
<Author>
<First_Name>Jennifer</First_Name>
<Last_Name>Widom</Last_Name>
</Author>
</Authors>
</Book>
<Book ISBN="ISBN-0-13-031995-3" Price="75">
<Title>Database Systems: The Complete Book</Title>
<Authors>
<Author>
<First_Name>Hector</First_Name>
<Last_Name>Garcia-Molina</Last_Name>
</Author>
<Author>
<First_Name>Jeffrey</First_Name>
<Last_Name>Ullman</Last_Name>
</Author>
<Author>
<First_Name>Jennifer</First_Name>
<Last_Name>Widom</Last_Name>
</Author>
</Authors>
<Remark>
Amazon.com says: Buy this book bundled with "A First Course,"
it’s a great deal!
</Remark>
</Book>
</Bookstore>

使用上面的XML,我想选择那些写过 "A First Course In Database Systems "和写过另一本书的作者。

for $b in /Bookstore/Book
let $a := $b/Authors/Author
let $t := $b/Title
where $t = "Database Systems: The Complete Book" and count($a)>1
return $a

但是这样会返回所有的作者,因为我认为这只是在计算该书的作者数量。我想计算每个独特的作者总共出现的次数,因为我认为这是应该采取的方法。

我怎样才能做到这一点呢?

xml xpath xquery
1个回答
2
投票

可以用纯XPath表达为

/Bookstore/Book[Title = 'A First Course in Database Systems']/Authors/Author[. = /Bookstore/Book[Title != 'A First Course in Database Systems']/Authors/Author]

0
投票

如果你还想用xquery来做,可以试试。

let 
$Book := $store//Book, 
$opus := "A First Course in Database Systems",
$authors := $Book[Title=$opus]/Authors/Author,
$others := $Book[Title!=$opus]/Authors/Author

Pre -edit:

for 
$author in $authors, 
$other in $others
where $author = $other
return $author

已编辑。

for $author in $authors
where $author = $others
return $author

我保留了 "之前 "和 "之后 "的位置,以便与马丁-霍能交换意见,下面,继续为未来的观众提供意义。

© www.soinside.com 2019 - 2024. All rights reserved.