0

我有如下输入请求

<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-->它由“业务标识符”的所有 B 值与“:”连接,然后对于每个业务对象,它用“|”分隔 Bkey--> 使用 bkey Bvalue 的直接映射 Bvalue--> 使用 Bvalue BOID 的直接映射-> 它必须为每个业务对象形成,需要将业务标识符的值 Bvalues 与 ':' 任何建议,我相信我必须这里有两个复杂的循环,但无法破解它。

谢谢

4

1 回答 1

0

随着查询

<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 中,您没有!简单的 map 运算符,但您应该可以使用它,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>
于 2018-11-22T14:10:03.100 回答