要求:
示例文档:
<purchase-order>
<line-items>
<line-item type=lamp amount=3500 </line-item>
<line-item type=couch amount=50000 </line-item>
<line-item type=chair amount=40000 </line-item>
</line-items>
<other-stuff></other-stuff>
</purchase-order>
答案 0 :(得分:0)
首先,您的xml格式不正确: 我已经在XML和查询下面使用了
let $input :=
<purchase-order>
<line-items>
<line-item type="lamp" amount="52"/>
<line-item type="lamp" amount="40000"/>
<line-item type="lamp" amount="3500"/>
<line-item type="lamp" amount="3500"/>
<line-item type="couch" amount="50000"/>
<line-item type="chair" amount="110000"/>
<line-item type="chair" amount="5000"/>
<line-item type="chair" amount="80000"/>
</line-items>
<other-stuff></other-stuff>
</purchase-order>
let $purchase-order :=
for $each-type in distinct-values($input/line-items/line-item/@type)
let $seq := $input/line-items/line-item[@type = $each-type]
order by $each-type
return
<line-items type="{$each-type}">
{
for $one in $seq
order by xs:integer($one/@amount)
return $one
}
</line-items>
return <purchase-order>{$purchase-order}</purchase-order>
通过查询生成的输出:
<purchase-order>
<line-items type="chair">
<line-item amount="5000" type="chair"/>
<line-item amount="80000" type="chair"/>
<line-item amount="110000" type="chair"/>
</line-items>
<line-items type="couch">
<line-item amount="50000" type="couch"/>
</line-items>
<line-items type="lamp">
<line-item amount="52" type="lamp"/>
<line-item amount="3500" type="lamp"/>
<line-item amount="3500" type="lamp"/>
<line-item amount="40000" type="lamp"/>
</line-items>
</purchase-order>
希望您的需求将完全填写此查询。