简体   繁体   English

如何计算 XML 中的元素数量?

[英]how to count the number of elements in XML?

I'm looking to get basic metadata about the data within a database.我正在寻找有关数据库中数据的基本元数据 Specifically, the number of line elements which are surrouned by the root text element.具体来说,被根text元素包围的line元素的数量。

Analogous to what I'd expect COUNT to return in SQL -- a single integer.类似于我期望COUNTSQL返回的内容——单个整数。

the database:数据库:

thufir@dur:~/flwor/group$ 
thufir@dur:~/flwor/group$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
> 
> open people
Database 'people' was opened in 225.24 ms.
> 
> xquery /
<text>
  <line>people</line>
  <line>joe</line>
  <line>phone1</line>
  <line>phone2</line>
  <line>phone3</line>
  <line>sue</line>
  <line>cell4</line>
  <line>home5</line>
  <line>alice</line>
  <line>atrib6</line>
  <line>x7</line>
  <line>y9</line>
  <line>z10</line>
</text>
Query executed in 215.13 ms.
> 
> exit
See you.
thufir@dur:~/flwor/group$ 

Counting the lines:计算行数:

thufir@dur:~/flwor/group$ 
thufir@dur:~/flwor/group$ basex each.xq 
1
2
3
4
5
6
7
8
9
10
11
12
13thufir@dur:~/flwor/group$ 

code:代码:

xquery version "3.0";

for $line in db:open("people")
for $index at $count in $line/text/line
return $count

我想你只是想使用count(/text/line)

From Martin's answer I see it's easy from within basex itself:从 Martin 的回答中,我认为在 basex 内部很容易:

thufir@dur:~/flwor/group$ 
thufir@dur:~/flwor/group$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
> 
> open people
Database 'people' was opened in 208.33 ms.
> 
> xquery /
<text>
  <line>people</line>
  <line>joe</line>
  <line>phone1</line>
  <line>phone2</line>
  <line>phone3</line>
  <line>sue</line>
  <line>cell4</line>
  <line>home5</line>
  <line>alice</line>
  <line>atrib6</line>
  <line>x7</line>
  <line>y9</line>
  <line>z10</line>
</text>
Query executed in 237.69 ms.
> 
> xquery count(/text/line)
13
Query executed in 20.55 ms.
> 
> exit
See you.
thufir@dur:~/flwor/group$ 

but I was also looking to run it from an xq file:但我也想从xq文件中运行它:

xquery version "3.0";

count(
for $line in db:open("people")
return $line/text/line)

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

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