使用XQuery计算具有特定条件的节点

时间:2016-02-18 12:21:08

标签: selection xquery

我试图回复出版商所有有声读物和印刷书籍的数量," Seagypsy Publishing"。我的XML示例如下:

<books>
    <book>
        <title>Tales of The Sea</title>
        <publisher>Seagypsy Publishing</publisher>
        <type>print</type>
    </book>
    <book>
        <title>Storm Shanties</title>
        <publisher>Seagypsy Publishing</publisher>
        <type>audio</type>
    </book>
    <book>
        <title>Fabulous Sea Monsters</title>
        <publisher>Seagypsy Publishing</publisher>
        <type>audio</type>
    </book>
    <book>
        <title>Pirates!</title>
        <publisher>Hobbes</publisher>
        <type>audio</type>
    </book>
</books>

这是我拥有的XQuery,虽然它正确地返回了音频和印刷书籍的数量,但它并不仅仅返回Seagypsy Publishing的那些。我怎么能纠正这个?以某种方式使用地图会更好吗?

xquery version "3.0";

for $books in doc("books.xml")/books
let $audio_count := count($books/book[type = 'audio'] 
    and $books/book[publisher = 'Seagypsy Publishing'])
let $print_count := count($books/book[type = 'print'] 
    and $books/book[publisher = 'Seagypsy Publishing'])
return 
    <seagypsy_count>
        <audio_books>{$audio_count}</audio_books>
        <print_books>{$print_count}</print_books>
    </seagypsy_count>   

1 个答案:

答案 0 :(得分:0)

您希望计算同一父级中同时包含子book和子type = 'audio'的{​​{1}}元素:

publisher = 'Seagypsy Publishing'