这是我的第二篇文章,我希望你能帮助我。我发现了类似的问题,但要么它们稍有不同,要么我无法将解决方案转移到我的问题上。
我确实有以下来源:
<?xml version="1.0" encoding="UTF-8"?>
<goods>
<item>
<groupIdentification>001</groupIdentification>
<measures>
<measure type="piece">
<value>100</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>100</value>
<unit>KG</unit>
</measure>
</measures>
</item>
<item>
<groupIdentification>002</groupIdentification>
<measures>
<measure type="piece">
<value>200</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>200</value>
<unit>KG</unit>
</measure>
</measures>
</item>
<item>
<groupIdentification>001</groupIdentification>
<measures>
<measure type="piece">
<value>300</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>300</value>
<unit>KG</unit>
</measure>
</measures>
</item>
</goods>
基于不同的组识别值,我想生成一个<groupedItem>
的实例。在<groupedItem>
中我想引用<groupIdentification>
(但只有一个值)并总结不同的度量或任何其他值,它们共享相同的组标识。
我想要实现的目标结构是:
<?xml version="1.0" encoding="UTF-8"?>
<goods>
<groupedItem>
<groupIdentification>001</groupIdentification>
<measures>
<measure type="piece">
<value>400</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>400</value>
<unit>KG</unit>
</measure>
</measures>
</groupedItem>
<groupedItem>
<groupIdentification>002</groupIdentification>
<measures>
<measure type="piece">
<value>200</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>200</value>
<unit>KG</unit>
</measure>
</measures>
</groupedItem>
</goods>
请帮我解释一下如何实现这一目标以及机制是什么。我有点难以告诉查询总结那些共享相同“分组元素”的东西。
提前致谢。
修改
如果我使用以下查询,我只会获得2 groupedItems
,但我无法在较低级别继续。
xquery version "3.0";
<goods>
{
for $item in goods/item
let $group := $item/groupIdentification
group by $group
return
<groupedItem>
{
for $groupIdentification in goods/item/groupIdentification
(: this is where I fail to get the connection :)
return
<groupIdentification>{ data($groupIdentification) }</groupIdentification>
}
(: no need for measure here yet, first need to solve the problem itself :)
</groupedItem>
}
</goods>
我明白了:
<?xml version="1.0" encoding="UTF-8"?>
<goods>
<groupedItem>
<groupIdentification>001</groupIdentification><groupIdentification>002</groupIdentification><groupIdentification>001</groupIdentification>
</groupedItem>
<groupedItem>
<groupIdentification>001</groupIdentification><groupIdentification>002</groupIdentification><groupIdentification>001</groupIdentification>
</groupedItem>
</goods>
答案 0 :(得分:1)
......但我在下层继续失败
您需要再次分组。除了unit
之外,您可能还希望对@type
进行分组。
示例...
XML输入(已修改为引入第二个单元)
<goods>
<item>
<groupIdentification>001</groupIdentification>
<measures>
<measure type="piece">
<value>100</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>220</value>
<unit>LB</unit>
</measure>
<measure type="weight">
<value>100</value>
<unit>KG</unit>
</measure>
</measures>
</item>
<item>
<groupIdentification>002</groupIdentification>
<measures>
<measure type="piece">
<value>200</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>200</value>
<unit>KG</unit>
</measure>
</measures>
</item>
<item>
<groupIdentification>001</groupIdentification>
<measures>
<measure type="piece">
<value>300</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>300</value>
<unit>KG</unit>
</measure>
<measure type="weight">
<value>661</value>
<unit>LB</unit>
</measure>
</measures>
</item>
</goods>
XQuery 3.0
xquery version "3.0";
<goods>{
for $item in /goods/item
let $key := $item/groupIdentification
group by $key
order by $key
return
<groupedItem>
<groupIdentification>{$key}</groupIdentification>
<measures>{
for $measure in $item/measures/measure
let $type := $measure/@type
let $unit := $measure/unit
group by $type,$unit
order by $type,$unit
return
<measure type="{$type}">{
<value>{sum($measure/value)}</value>,
<unit>{$unit}</unit>
}</measure>
}</measures>
</groupedItem>
}</goods>
<强>输出强>
<goods>
<groupedItem>
<groupIdentification>001</groupIdentification>
<measures>
<measure type="piece">
<value>400</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>400</value>
<unit>KG</unit>
</measure>
<measure type="weight">
<value>881</value>
<unit>LB</unit>
</measure>
</measures>
</groupedItem>
<groupedItem>
<groupIdentification>002</groupIdentification>
<measures>
<measure type="piece">
<value>200</value>
<unit>PK</unit>
</measure>
<measure type="weight">
<value>200</value>
<unit>KG</unit>
</measure>
</measures>
</groupedItem>
</goods>