如何计算XML中的元素个数?
how to count the number of elements in XML?
我正在寻找有关数据库中 data 的基本元数据。具体来说,根 text
元素包围的 line
元素的数量。
类似于我期望的 COUNT
到 SQL
中的 return -- 一个整数。
数据库:
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
我想您只是想使用 count(/text/line)
。
从 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$
但我也在 运行 从 xq
文件中寻找它:
xquery version "3.0";
count(
for $line in db:open("people")
return $line/text/line)
我正在寻找有关数据库中 data 的基本元数据。具体来说,根 text
元素包围的 line
元素的数量。
类似于我期望的 COUNT
到 SQL
中的 return -- 一个整数。
数据库:
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
我想您只是想使用 count(/text/line)
。
从 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$
但我也在 运行 从 xq
文件中寻找它:
xquery version "3.0";
count(
for $line in db:open("people")
return $line/text/line)