Eclipse Xquery用于复杂有效负载

时间:2018-11-29 12:10:20

标签: eclipse xquery oracle-service-bus

我有如下输入请求

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

对于上述有效载荷 输出应为

  <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的直接映射 BOID->必须为每个业务对象组成,需要将业务标识符Bvalues的值与':'连接起来 任何建议,我相信我必须在这里进行两个复杂的循环,但无法破解。

感谢@Martin,因为他帮助我使用了'ancestor'轴来破解它,但是eclispe如何无法识别'ancestor'。

谢谢

1 个答案:

答案 0 :(得分:0)

所有输出的BIKey都相同,因此我们可以在任何循环之外进行计算并完成它。为此,我们将BusinessObject的所有BValue与:结合在一起,并将每个BusinessObject的结果与|结合起来:

let $BIKey := string-join(for $bo in /Input/BusinessObjects/BusinessObject return string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, ':'), '|')

BOID将取决于我们正在处理的BusinessObject,因此我们需要对它们进行循环,然后可以在此时提取BOID:

let $BIKEY := [...]
for $bo in /Input/BusinessObjects/BusinessObject
  let $BOID := string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, ':')

其余的输出字段取决于BusinessIdentifier,所以让我们也对它们进行循环并为每个BusinessIdentifier返回一个BusinessObject:

let $BIKEY := [...]
for $bo in [...]
  let $BOID :=  [...]
  return for $bi in $bo/BusinessIdentifiers/BusinessIdentifier/
    return <BusinessObject>
             <BIKey>{$BIKEY}</BIKey>
             <BKey>{$bi/BKey}</BKey>
             <Bvalue>{$bi/Value}</Bvalue>
             <BOID>{$BOID}</BOID>
           </BusinessObject>

现在我们所需要做的就是将这些输出BusinessObjects包装到其父标记中:

<output><BusinessObjects>{
let $BIKEY := string-join(for $bo in /Input/BusinessObjects/BusinessObject return string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, ':'), '|')
for $bo in /Input/BusinessObjects/BusinessObject
  let $BOID := string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, ':')
  return
    for $bi in $bo/BusinessIdentifiers/BusinessIdentifier
    return <BusinessObject>
<BIKey>{$BIKEY}</BIKey>
<BKey>{$bi/BKey/text()}</BKey>
<Bvalue>{$bi/BValue/text()}</Bvalue>
<BOID>{$BOID}</BOID>
</BusinessObject>
}</BusinessObjects></output>

您可以test it here