加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – XQuery连接结果

发布时间:2020-12-16 22:41:11 所属栏目:百科 来源:网络整理
导读:我有一个看起来像这样的 XML: ?xml version="1.0"?root flight number10001/number airportLAX/airport dest airportSFO/airport /dest /flight flight number10002/number airportLAX/airport dest airportJFK/airport /dest /flight flight number10003/n
我有一个看起来像这样的 XML:

<?xml version="1.0"?>
<root>
  <flight>
    <number>10001</number>
    <airport>LAX</airport>
    <dest>
      <airport>SFO</airport>
    </dest>
  </flight>
  <flight>
    <number>10002</number>
    <airport>LAX</airport>
    <dest>
      <airport>JFK</airport>
    </dest>
  </flight>
  <flight>
    <number>10003</number>
    <airport>JFK</airport>
    <dest>
      <airport>LAX</airport>
    </dest>
  </flight>
</root>

使用XQuery我需要得到这样的东西:

<res>
    <airport code="LAX">
        <deps>2</deps>
        <dests>1</deps>
    </airport>
    <airport code="JFK">
        <deps>1</deps>
        <dests>1</deps>
    </airport>
    <airport code="SFO">
        <deps>0</deps>
        <dests>1</deps>
    </airport>
</res>

我做到了,并且可以得到正确的结果,但是,我的查询只能找到deps或dests,但不能同时找到两者.

以下是我解决问题的方法.

let $all := doc("flights.xml")/root
for $airports in distinct-values($all/flight//*/airport) (:here I get all airport codes:)
order by $airports 

for $nr-dep in $all/flight/airport

where $nr-dep = $airports 
group by $airports 

return <res>
          <airport name="{$airports}"><deps>{count($nr-dep)}</deps></airport>
       </res>

我在这里得到了离境数.我可以通过在$all / flight / dest / airport中以$nr-dep替换$nr-dep来轻松获得destionation但是我无法找到一种方法来显示与预期相同的结果XML.

解决方法

为什么不简单:

for $airport in distinct-values($all//airport)
order by $airport
return <airport code="{$airport}">
  <deps>{count($all//flight/airport[. = $airport])}</deps>
  <dests>{count($all//dest/airport[. = $airport])}</dests>
</airport>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读