Xquery用于复杂有效负载

时间:2018-11-16 12:51:15

标签: 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>

但是我没有满足确切的要求。请提出如何进行。

谢谢

1 个答案:

答案 0 :(得分:0)

看起来就是您想要的:

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

您可以try it here

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

  • 您创建了一个<BIKey>,而您期望每个BusinessObject =>将<BIKey>标签移到FLWOR的return中,而不是移到标签外
  • 您有一个string-join使用|作为分隔符
  • 您只加入了BValue内的BusinessIdentifier s,但您也希望加入BusinessAttribute s内的那些人