使用 XQuery 对符合特定条件的节点进行计数

Using XQuery to count nodes with a certain criteria

我正在尝试 return 由出版商 "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,虽然它正确地 return 统计了音频和印刷书籍,但它 return 不只是 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>   

您想要计算 book 个在同一父级中同时具有子级 type = 'audio' 和子级 publisher = 'Seagypsy Publishing' 的元素:

.....
let $audio_count := count($books/book[type = 'audio' and publisher = 'Seagypsy Publishing'])
let $print_count := count($books/book[type = 'print' and publisher = 'Seagypsy Publishing'])
.....