嵌套有效负载xml的Xquery

问题描述 投票:0回答:1

这是我的输入XML:

这是我修改的xml数据作为输入

 <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>

我想将以下输出CDC|123|857895:CDC|123|34567分配给<BIKey>

我按照Martin的建议尝试了这个Xquery,它实际上解决了我的ealrier问题,但是我的inputpayload与我之前的问题相比更多:

<Input>  
    For $BusinessObject in Input/BusinessObjects/BusinessObject[1]
    retrun


      <BIKey>{ string-join(Input/BusinessObjects/BusinessObject[1]/BusinessIdentifiers/BusinessIdentifier/BValue, '|') }</BIKey>


    </Input>

但我得到了这个输出

CDC|123|857895

请协助,因为我不知道在哪里循环有效载荷以获得所需的输出。

谢谢

xquery
1个回答
0
投票

只需使用string-join两次,一次调用嵌套到另一次:

<BIKey>
{
    string-join(
        Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|'),
        ':'
    )
}
</BIKey>

这样,外部string-join连接条形分隔弦的序列,内部Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|')返回一个冒号。

https://xqueryfiddle.liberty-development.net/eiQZDbi的例子。

如果您没有XQuery 3或更高版本,则可以使用!替换

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

https://xqueryfiddle.liberty-development.net/eiQZDbi/2

© www.soinside.com 2019 - 2024. All rights reserved.