我正在尝试计算类似的“itemid_ref”,以获取其中每个items ref的编号:
<?xml version="1.0" encoding="utf-8"?>
<electrystore>
<itembill>
<itembill_id>1</itembill_id>
<itemid_ref>2</itemid_ref>
<billid_ref>2</billid_ref>
<price>20000</price>
</itembill>
<itembill>
<itembill_id>2</itembill_id>
<itemid_ref>3</itemid_ref>
<billid_ref>3</billid_ref>
<price>250000</price>
</itembill>
<itembill>
<itembill_id>3</itembill_id>
<itemid_ref>3</itemid_ref>
<billid_ref>3</billid_ref>
<price>30000</price>
</itembill>
<itembill>
<itembill_id>4</itembill_id>
<itemid_ref>1</itemid_ref>
<billid_ref>2</billid_ref>
<price>140000</price>
</itembill>
<itembill>
<itembill_id>5</itembill_id>
<itemid_ref>2</itemid_ref>
<billid_ref>1</billid_ref>
<price>10000</price>
</itembill>
</electrystore>
我试图将xquery的输出如下:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<itemid_ref>
2
</itemid_ref>
<numberOfitemes>2</numberOfitemes>
</result>
<result>
<itemid_ref>
3
</itemid_ref>
<numberOfitemes>2</numberOfitemes>
</result>
<result>
<itemid_ref>
1
</itemid_ref>
<numberOfitemes>1</numberOfitemes>
</result>
我该怎么做?
最佳答案
XQuery1.0中没有group by
,但通常可以通过使用distinct-values
,只要数据集不太大。
for $itemid_ref in distinct-values(/electrystore/itembill/itemid_ref)
let $count := count(/electrystore/itembill[itemid_ref = $itemid_ref])
return element result {
element itemid_ref { $itemid_ref },
element numberOfitemes { $count }
}
要在大型数据集上执行,您可能需要查看xquery处理器中允许访问索引的扩展。
在XQuery3.0中有点简单:
for $itembill in /electrystore/itembill
let $itemid_ref := $itembill/itemid_ref
group by $itemid_ref
return element result {
element itemid_ref { $itemid_ref },
element numberOfitemes { count($itembill) }
}
关于xml - Xquery中的分组依据和计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20483524/