【发布时间】:2021-08-25 20:04:20
【问题描述】:
基于Count number of elements with same tag
我将使用 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 := '	' (: tab :)
for $elems in doc(
'books'
)/bookstore/book/*
let $currname := name(
$elems
)
group by $currname
return string-join(
(
$currname
),
$sep
)
也就是说,
title
author
我想我想用count(),但我不知道该怎么说我要数父书。在上面提到的我回答的问题中,要计数的元素名称在查询中是硬编码的。在这种情况下,我使用的是通配符。
【问题讨论】:
标签: xml grouping xquery counting basex