完成多个 for 循环的 Xquery

Completed Xquery for multiple for loops

我有如下输入请求

<Input>
<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>
       </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>
        </BusinessObject>      
    </BusinessObjects>
    </Input>

我需要形成如下模式的输出

<Output>
<BusinessObject>
<BIKey></BIKey>
<BKey></BIKey>
<Bvalue></Bvalue>
<BOID></BOID>
</BusinessObject>
</Output>

对于上面的payload 输出应该是

<Output>
<BusinessObjects>
<BusinessObject>
<BIKey>CDC:123:857895|CDC:123:34567</BIKey>
<BKey>BUCode</BKey>
<Bvalue>CDC</Bvalue>
<BOID>CDC:123:857895</BOID>
</BusinessObject>
<BusinessObject>
<BIKey>CDC:123:857895|CDC:123:34567</BIKey>
<BKey>BUtype</BKey>
<Bvalue>123</Bvalue>
<BOID>CDC:123:857895</BOID>
</BusinessObject>
<BusinessObject>
<BIKey>CDC:123:857895|CDC:123:34567</BIKey>
<BKey>CSMNo</BKey>
<Bvalue>857895</Bvalue>
<BOID>CDC:123:857895</BOID>
</BusinessObject>
<BusinessObject>
<BIKey>CDC:123:857895|CDC:123:34567</BIKey>
<BKey>BUCode</BKey>
<Bvalue>CDC</Bvalue>
<BOID>CDC:123:34567</BOID>
</BusinessObject>
<BusinessObject>
<BIKey>CDC:123:857895|CDC:123:34567</BIKey>
<BKey>BUtype</BKey>
<Bvalue>123</Bvalue>
<BOID>CDC:123:34567</BOID>
</BusinessObject>
<BusinessObject>
<BIKey>CDC:123:857895|CDC:123:34567</BIKey>
<BKey>CSMNo</BKey>
<Bvalue>857895</Bvalue>
<BOID>CDC:123:34567</BOID>
</BusinessObject>
</BusinessObjects>
</Output>

我已经尝试在 Xquery 下获得相同的结果,但最终出现错误或不符合要求

<Ouput>
<BusinessObjects>
{
for $bi in Input/BusinessObjects/BusinessObject/BusinessIdentifiers/BusinessIdentifier
return
<BIKey>
    {
        string-join(
            for $bo in Input/BusinessObjects/BusinessObject return string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, '|'),
            ':'
        )
    }
    </BIKey>
    <BKey>data {$bi/Bkey}</BKey>
    <Bvalue>data {$bi/Bvalue}</Bvalue>
    for $bo in Input/BusinessObjects/BusinessObject return <BOID>{string-join($bo//BValue, ':')}<BOID>

}
</BusinessObjects>
</Ouput>

输出字段说明如下 BIKey-->它由 'Business Identifier' 的所有 Bvalues 与“:”连接而成,然后对于每个 businessobject,它用“|”分隔 Bkey-->直接用bkey映射 Bvalue-->用Bvalue直接映射 BOID--> 它必须为每个业务对象形成,需要将业务标识符的值 B 值与“:”连接起来 任何建议,我相信我必须在这里有两个复杂的循环,但无法破解它。

谢谢

随查询

<Output>
    <BusinessObjects>
        {
            //BusinessIdentifier 
            ! 
            <BusinessObject>
                <BIKey>{string-join(ancestor::BusinessObjects/BusinessObject!string-join(.//BValue, ':'), '|')}</BIKey>
                {
                    BKey, 
                    BValue
                }
                <BOID>{ancestor::BusinessObject!string-join(.//BValue, ':')}</BOID>
            </BusinessObject>
        }
    </BusinessObjects>
</Output>

https://xqueryfiddle.liberty-development.net/948Fn5g我得到了结果

<Output>
   <BusinessObjects>
      <BusinessObject>
         <BIKey>CDC:123:857895|CDC:123:34567</BIKey>
         <BKey>BuCode</BKey>
         <BValue>CDC</BValue>
         <BOID>CDC:123:857895</BOID>
      </BusinessObject>
      <BusinessObject>
         <BIKey>CDC:123:857895|CDC:123:34567</BIKey>
         <BKey>BuType</BKey>
         <BValue>123</BValue>
         <BOID>CDC:123:857895</BOID>
      </BusinessObject>
      <BusinessObject>
         <BIKey>CDC:123:857895|CDC:123:34567</BIKey>
         <BKey>CsmNo</BKey>
         <BValue>857895</BValue>
         <BOID>CDC:123:857895</BOID>
      </BusinessObject>
      <BusinessObject>
         <BIKey>CDC:123:857895|CDC:123:34567</BIKey>
         <BKey>BuCode</BKey>
         <BValue>CDC</BValue>
         <BOID>CDC:123:34567</BOID>
      </BusinessObject>
      <BusinessObject>
         <BIKey>CDC:123:857895|CDC:123:34567</BIKey>
         <BKey>BuType</BKey>
         <BValue>123</BValue>
         <BOID>CDC:123:34567</BOID>
      </BusinessObject>
      <BusinessObject>
         <BIKey>CDC:123:857895|CDC:123:34567</BIKey>
         <BKey>CsmNo</BKey>
         <BValue>34567</BValue>
         <BOID>CDC:123:34567</BOID>
      </BusinessObject>
   </BusinessObjects>
</Output>

在 XQuery 1 中,您没有 ! 简单映射运算符,但您应该可以使用 for .. return,请参阅 https://xqueryfiddle.liberty-development.net/948Fn5g/1

<Output>
    <BusinessObjects>
        {
            for $bi in //BusinessIdentifier 
            return  
            <BusinessObject>
                <BIKey>{string-join($bi/ancestor::BusinessObjects/BusinessObject/string-join(.//BValue, ':'), '|')}</BIKey>
                {
                    $bi/BKey, 
                    $bi/BValue
                }
                <BOID>{$bi/ancestor::BusinessObject/string-join(.//BValue, ':')}</BOID>
            </BusinessObject>
        }
    </BusinessObjects>
</Output>

或构造新元素的简单步骤,参见https://xqueryfiddle.liberty-development.net/948Fn5g/2

<Output>
    <BusinessObjects>
        {
            //BusinessIdentifier/
            <BusinessObject>
                <BIKey>{string-join(ancestor::BusinessObjects/BusinessObject/string-join(.//BValue, ':'), '|')}</BIKey>
                {
                    BKey, 
                    BValue
                }
                <BOID>{ancestor::BusinessObject/string-join(.//BValue, ':')}</BOID>
            </BusinessObject>
        }
    </BusinessObjects>
</Output>