计算具有相同标签的元素数量 [英] Count number of elements with same tag
本文介绍了计算具有相同标签的元素数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基于 books.xml 转换为 CSV:在每一行重复标题:
对于下面的文档,我怎么算
For the document below, how can I count
- 每本书的作者数量
- 每本书的唯一作者数量?
在这种情况下,它们都是相同的:
In this case, they would both be the same:
+--------------------+---------------+
| | |
| Title | Author_count |
+--------------------+---------------+
| | |
| Everyday Italian | 1 |
+--------------------+---------------+
| | |
| Harry Potter | 1 |
+--------------------+---------------+
| | |
| XQuery Kick Start | 5 |
+--------------------+---------------+
| | |
| Learning XML | 1 |
+--------------------+---------------+
xslt
或 xquery
方法都可以.我更喜欢表格输出.
xslt
or xquery
approaches ar both OK. I would prefer tabular output.
<?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>
推荐答案
我使用的是 BaseX v.9.5.2
I am using BaseX v.9.5.2
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>
{
for $x in ./bookstore/book
return <r>
<title>{data($x/title)}</title>
<author_count>{count($x/author)}</author_count>
</r>
}
</root>
输出
<root>
<r>
<title>Everyday Italian</title>
<author_count>1</author_count>
</r>
<r>
<title>Harry Potter</title>
<author_count>1</author_count>
</r>
<r>
<title>XQuery Kick Start</title>
<author_count>5</author_count>
</r>
<r>
<title>Learning XML</title>
<author_count>1</author_count>
</r>
</root>
这篇关于计算具有相同标签的元素数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文