首页 > 解决方案 > 用于复杂负载的 Xquery

问题描述

我有如下输入请求

<Input>
<BIKey></BIKey>
<BusinessObjects>
      <BusinessObject>
        <BusinessIdentifiers>
          <BusinessIdentifier>
            <BKey>BuCode</BKey>
            <BValue>CDC</BValue>
          </BusinessIdentifier>
          <BusinessIdentifier>
            <BKey>BuType</BKey>
            <BValue>123</BValue>
          </BusinessIdentifier>
          <BusinessIdentifier>
            <BKey>CsmNo</BKey>
            <BValue>857895</BValue>
          </BusinessIdentifier>
        </BusinessIdentifiers>
        <BusinessAttributes>
          <BusinessAttribute>
            <BKey>Version</BKey>
            <BValue>1</BValue> 
          </BusinessAttribute>
          <BusinessAttribute>
            <BKey>date</BKey>
            <BValue>2018-06-28</BValue>
          </BusinessAttribute>
        </BusinessAttributes>
      </BusinessObject>
      <BusinessObject>
        <BusinessIdentifiers>
          <BusinessIdentifier>
            <BKey>BuCode</BKey>
            <BValue>CDC</BValue>
          </BusinessIdentifier>
          <BusinessIdentifier>
            <BKey>BuType</BKey>
            <BValue>123</BValue>
          </BusinessIdentifier>
          <BusinessIdentifier>
            <BKey>CsmNo</BKey>
            <BValue>34567</BValue>
          </BusinessIdentifier>
        </BusinessIdentifiers>
        <BusinessAttributes>
          <BusinessAttribute>
            <BKey>Version</BKey>
            <BValue>1</BValue> 
          </BusinessAttribute>
          <BusinessAttribute>
            <BKey>date</BKey>
            <BValue>2018-06-28</BValue>
          </BusinessAttribute>
        </BusinessAttributes>
      </BusinessObject>      
    </BusinessObjects>
    </Input>

并且我希望该<BIKey>值应该是<BValue>每个<BusinessObject>用“:”分隔的所有值,对于上面的示例,<BIKey>值应该填充如下

<BIKey>CDC:123:857895:1:2018-06-28</BIKey>
<BIKey>CDC:123:34567:1:2018-06-28</BIKey>

我试过如下

 <BIKey>
    {
        string-join(
            for $bo in Input/BusinessObjects/BusinessObject return string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, '|'),
            ':'
        )
    }
    </BIKey>

但我没有达到确切的要求。请建议如何进行。

谢谢

标签: xquery

解决方案


看起来这是你想要的:

for $bo in Input/BusinessObjects/BusinessObject return <BIKey>{string-join($bo//BValue, ':')}</BIKey>

你可以在这里试试

您的 XQuery 代码存在以下问题:

  • 您创建了一个<BIKey>,而您期望每个BusinessObject=> 将<BIKey>标签移动return到 FLWOR 的而不是它的外部
  • 你有一个string-join用作|分隔符
  • 你只加入了BValues inside BusinessIdentifiers,但你也想要那些 inside BusinessAttributes

推荐阅读