繁体   English   中英

XQuery 3:计算文档中元素名称的出现次数

[英]XQuery 3: Count occurrences of element names across document

基于计数具有相同标签的元素数量

我将使用 BaseX 9.5.2 运行此查询。

鉴于数据

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
    </book>
    <book category="WEB">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
    </book>
</bookstore>

我想生成一个这样的表

+----------+--------------+
|          |              |
| Element  | total_count  |
+----------+--------------+
|          |              |
| title    | 4            |
+----------+--------------+
|          |              |
| author   | 8            |
+----------+--------------+

我可以得到一个唯一元素名称的列表

let $sep := '&#09;' (: tab :)

for $elems in doc(
  'books'
)/bookstore/book/*

let $currname := name(
  $elems
)

group by $currname

return string-join(
       (
        $currname
       ),
       $sep
)

那是,

title
author

我想我想使用count() ,但我不知道怎么说我想计算父书。 在上面提到的我回答的问题中,要计数的元素名称在查询中是硬编码的。 在这种情况下,我使用的是通配符。

由于您已经拥有分组, count($elems)将在return子句中具有正确的值。

我认为您最初使用let $sep会导致问题,我建议的分组count($elems)https://xqueryfiddle.liberty-development.net/bFDbxm7对我来说很好,我已将$sep移至声明的变量。

我正在使用 BaseX v.9.5.2

请尝试以下 XQuery。

XQuery

xquery version "3.1";

declare context item := document {
<bookstore>
    <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
    </book>
    <book category="WEB">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
    </book>
</bookstore>
};

<root>
{
  let $title := count(./bookstore/book/title)
  let $author := count(distinct-values(./bookstore/book/author))
  return <r>
    <title>{$title}</title>
    <unique_author_count>{$author}</unique_author_count>
  </r>
}
</root>

XQuery #2

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'text';
declare option output:item-separator '&#10;';
    
declare context item := document {
<bookstore>
    <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
    </book>
    <book category="WEB">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
    </book>
</bookstore>
};

let $sep := '&#09;' (: tab :)

for $elems in ./bookstore/book/*
let $currname := local-name($elems)
group by $currname
return string-join(($currname, count(distinct-values($elems[local-name()=$currname])),$sep))

暂无
暂无

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

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