繁体   English   中英

XQuery - 使用带有 WHERE 条件的 GROUP BY 来检索 XML 文件名

[英]XQuery - using GROUP BY with WHERE condition to retrieve XML file names

我在创建 XQuery 3.0(使用 BaseX 9.7)来检索满足某些条件的集合中的 XML 文件的名称时遇到问题:如果另一个元素(“作者”)具有某个价值。

简化的 XML 示例:

“好.xml”:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <!-- OK -->
    <books>
        <book>
            <author>Alice</author>
            <type>mystery</type>
        </book>
        <book>
            <author>Alice</author>
            <type>fantasy</type>
        </book>
    </books>

“not_good.xml”:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- NOT OK -->
<books>
    <book>
        <author>Alice</author>
        <type>horror</type>
    </book>
    <book>
        <author>Alice</author>
        <type>horror</type>
    </book>
</books>

如果可能的“类型”数量很少,我可以使用以下 XQuery:

for $books in collection('group_test')/books[
       count(.//book[author = 'Alice'][type = 'mystery']) > 1
    or count(.//book[author = 'Alice'][type = 'fantasy']) > 1
    or count(.//book[author = 'Alice'][type = 'horror']) > 1
]
return db:path($books)

我尝试了以下 XQuery,但没有找到任何东西:

for $books in collection('group_test')/books[/book/author = 'Alice']
group by $type := //book/type 
where count($type) > 1

return db:path($books)

也许

for $book in collection('group_test')/books/book[author = 'Alice']
group by $type := $book/type 
where count($book) > 1

return ($book!db:path(.))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM