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

问题描述 投票:0回答:2

我正在寻找有关数据库中data的基本元数据。具体来说,是由根line元素替代的text元素的数量。

类似于我期望COUNTSQL返回的值—一个整数。

数据库:

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$ 

行数:

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$ 

代码:

xquery version "3.0";

for $line in db:open("people")
for $index at $count in $line/text/line
return $count
text count xquery basex flwor
2个回答
1
投票

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


0
投票

我看到:

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$ 

但是我也想从xq文件运行它:

thufir@dur:~/flwor/group$ 
thufir@dur:~/flwor/group$ basex count.xq 
Stopped at /home/thufir/flwor/group/count.xq, 4/15:
[XPDY0002] root(): no context value bound.
thufir@dur:~/flwor/group$ 
thufir@dur:~/flwor/group$ cat count.xq 
xquery version "3.0";

for $line in db:open("people")
return count(/text)
thufir@dur:~/flwor/group$ 
© www.soinside.com 2019 - 2024. All rights reserved.