【发布时间】:2017-09-19 21:01:05
【问题描述】:
输入文件:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="6636551">
<master_information>
<book_xref>
<xref type="Fiction" type_id="1">72771KAM3</xref>
<xref type="Non_Fiction" type_id="2">US72771KAM36</xref>
</book_xref>
</master_information>
<book_details>
<price>24.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book_details>
</book>
<book id="119818569">
<master_information>
<book_xref>
<xref type="Fiction" type_id="1">070185UL5</xref>
<xref type="Non_Fiction" type_id="2">US070185UL50</xref>
</book_xref>
</master_information>
<book_details>
<price>19.25</price>
<publish_date>2002-11-01</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book_details>
</book>
<book id="119818568">
<master_information>
<book_xref>
<xref type="Fiction" type_id="1">070185UK7</xref>
<xref type="Non_Fiction" type_id="2">US070185UK77</xref>
</book_xref>
</master_information>
<book_details>
<price>5.95</price>
<publish_date>2004-05-01</publish_date>
<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
</book_details>
</book>
<book id="119818567">
<master_information>
<book_xref>
<xref type="Fiction" type_id="1">070185UJ0</xref>
<xref type="Non_Fiction" type_id="2">US070185UJ05</xref>
</book_xref>
</master_information>
<book_details>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</description>
</book_details>
</book>
</books>
我编写了 XQuery 来显示特定字段的计数,如下所示:
for $x in //book_xref
let $c := string-join(('name of element:', count($x)), ' ')
return $c
预期输出:
name of element: 4
但输出结果为:
name of element:
1
name of element:
1
name of element:
1
name of element:
1
之后我明白了它为什么这样做。我尝试汇总计数值,但没有成功。此外,找不到任何函数来自动获取元素的名称,以便它自动包含在字符串中。
理想情况下,目标输出是
book_xref:4
我需要什么来实现这一目标?我错过了什么?
谢谢!我很欣赏你的回应。
【问题讨论】: