将类似元素分组为xquery

时间:2012-11-22 11:02:32

标签: xquery

感谢您的快速回复。我试过了两个答案。它没有像我预期的那样工作。请找到以下输入和预期输出。在此先感谢

输入: <Billing> <billSummary>         <billTo>             <accountNumber> 130212192 </accountNumber>             <MSISDN> {1234567890 {1}}         </MSISDN>         </billTo>             <tax> {100.12 {1}}             <amount> {税{1}}         </amount>         <description>             </description> {ONE_TIME {1}}             </tax> {100.12 {1}}             <charge> {ONE_TIME_CHARGE {1}}         <typeCode>         </typeCode>             <amount>存取</amount>             <description> {100.12 {1}}             </description> {ACCESS_CHARGE {1}}         </charge>         <charge>             <typeCode> {100.12 {1}}             </typeCode> {税{1}}         <amount>         </amount>             <description>功能</description>             </charge> {100.12 {1}}             <tax> {FEATURE_CHARGE {1}}         <amount>         </amount>             <description> {100.12 {1}}             </description>调整</tax>         <charge>         <typeCode>             </typeCode> {开播{1}}             <amount> {100.12 {1}}             </amount> {AIRTIME_CHARGE {1}}         <description>         </description>             </charge> {LONG_DISTANCE {1}}             <deduction> {100.12 {1}}             <amount> {LONG_DISTANCE_CHARGE {1}}         </amount>         <description>             </description> {漫游{1}}             </deduction> {100.12 {1}}             <charge> {ROAMING_CHARGE {1}}         <typeCode>         </typeCode>             <amount> {100.12 {1}}             </amount>调整<description>         </description> </charge> <charge>         <typeCode>             </typeCode> 130212192 <amount>             </amount> {1234567890 {1}}         <description>         </description>             </charge> {100.12 {1}}             <charge> {税{1}}         <typeCode>         </typeCode>             <amount> {ONE_TIME {1}}             </amount> {100.12 {1}}             <description> {ONE_TIME_CHARGE {1}}         </description>         </charge>             <deduction>存取<amount>             </amount> {100.12 {1}}             <description> {ACCESS_CHARGE {1}}         </description>         </deduction>             </billSummary> {100.12 {1}}             <billSummary> {税{1}}         <billTo>         <accountNumber>             </accountNumber>功能<MSISDN>             </MSISDN> {100.12 {1}}             </billTo> {FEATURE_CHARGE {1}}         <tax>         <amount>             </amount> {100.12 {1}}             <description>调整</description>         </tax>         <charge>             <typeCode> {开播{1}}             </typeCode> {100.12 {1}}             <amount> {AIRTIME_CHARGE {1}}         </amount>         <description>             </description> {LONG_DISTANCE {1}}             </charge> {100.12 {1}}             <charge> {LONG_DISTANCE_CHARGE {1}}         <typeCode>         </typeCode>             <amount> {漫游{1}}             </amount> {100.12 {1}}             <description> {ROAMING_CHARGE {1}}         </description>         </charge>             <tax> {100.12 {1}}             <amount>调整</amount>         <description> </description> </tax>

预期产出: <charge> <typeCode>     </typeCode>         <amount> 130212192 </amount>         <description> {1234567890 {1}}     </description>     </charge>         <deduction> {100.12 {1}}         <amount> {税{1}}     </amount>     <description>         </description> {ONE_TIME {1}}         </deduction> {100.12 {1}}         <charge> {ONE_TIME_CHARGE {1}}     <typeCode>     </typeCode>         <amount>存取</amount>         <description> {100.12 {1}}         </description> {ACCESS_CHARGE {1}}     </charge>     <charge>         <typeCode> {100.12 {1}}         </typeCode> {税{1}}     <amount>     </amount>         <description>功能</description>         </charge> {100.12 {1}}         <charge> {FEATURE_CHARGE {1}}     <typeCode>     </typeCode>         <amount> {100.12 {1}}         </amount>调整<description>     </description>     </charge>         <deduction> {开播{1}}         <amount> {100.12 {1}}         </amount> {AIRTIME_CHARGE {1}}     <description>     </description>         </deduction> {LONG_DISTANCE {1}}         </billSummary> {100.12 {1}}         </Billing> {LONG_DISTANCE_CHARGE {1}}     <Billing>     <billSummary>         <billTo> {漫游{1}}         <accountNumber> {100.12 {1}}         </accountNumber> {ROAMING_CHARGE {1}}     <MSISDN>     </MSISDN>         </billTo> {100.12 {1}}         <tax>调整<amount>     </amount> <description> </description>     </tax>         <charge> 130212192 <typeCode>         </typeCode> {1234567890 {1}}     <amount>     </amount>         <description> {100.12 {1}}         </description> {税{1}}     </charge>     <charge>         <typeCode> {ONE_TIME {1}}         </typeCode> {100.12 {1}}         <amount> {ONE_TIME_CHARGE {1}}     </amount>     <description>         </description>存取</charge>         <tax> {100.12 {1}}         <amount> {ACCESS_CHARGE {1}}     </amount>     <description>         </description> {100.12 {1}}         </tax> {税{1}}     <charge>     <typeCode>         </typeCode>功能<amount>         </amount> {100.12 {1}}         <description> {FEATURE_CHARGE {1}}     </description>     </charge>         <deduction> {100.12 {1}}         <amount>调整</amount>     <description>     </description>         </deduction> {开播{1}}         <charge> {100.12 {1}}         <typeCode> {AIRTIME_CHARGE {1}}     </typeCode>     <amount>         </amount> {LONG_DISTANCE {1}}         <description> {100.12 {1}}         </description> {LONG_DISTANCE_CHARGE {1}}     </charge>     <charge>         <typeCode> {漫游{1}}         </typeCode> {100.12 {1}}         <amount> {ROAMING_CHARGE {1}}     </amount>     <description>         </description> {100.12 {1}}         </charge>调整<charge>     <typeCode> </typeCode> <amount>

2 个答案:

答案 0 :(得分:0)

按节点名称排序:

<billSummary>
{
  for $i in /billSummary/*
  order by $i/name()
  return $i
}
</billSummary>

如果您只想对每个帐单中的项目进行分组,请遍历所有<billSummary/> - 节点:

for $bill in //billSummary
return
  <billSummary>
  {
    for $item in $bill/*
    order by $item/name()
    return $item
  }
  </billSummary>

如果你需要在答案中包含一些节点,你应该能够自己做;您的问题不包含必要的信息。

答案 1 :(得分:0)

这个怎么样:

doc("c:\temp\temp.xml")/
<billSummary>
  {
  for $e in billSummary/*
  order by local-name($e)
  return $e
  }
</billSummary>