计算具有相同标签的元素的数量
Count number of elements with same tag
基于:
下面这个文件,我怎么算
- 每本书的作者人数
- 每本书的唯一作者数量?
在这种情况下,它们是相同的:
+--------------------+---------------+
| | |
| Title | Author_count |
+--------------------+---------------+
| | |
| Everyday Italian | 1 |
+--------------------+---------------+
| | |
| Harry Potter | 1 |
+--------------------+---------------+
| | |
| XQuery Kick Start | 5 |
+--------------------+---------------+
| | |
| Learning XML | 1 |
+--------------------+---------------+
xslt
或 xquery
方法都可以。我更喜欢表格输出。
<?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
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>
基于
下面这个文件,我怎么算
- 每本书的作者人数
- 每本书的唯一作者数量?
在这种情况下,它们是相同的:
+--------------------+---------------+
| | |
| Title | Author_count |
+--------------------+---------------+
| | |
| Everyday Italian | 1 |
+--------------------+---------------+
| | |
| Harry Potter | 1 |
+--------------------+---------------+
| | |
| XQuery Kick Start | 5 |
+--------------------+---------------+
| | |
| Learning XML | 1 |
+--------------------+---------------+
xslt
或 xquery
方法都可以。我更喜欢表格输出。
<?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
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>