[英]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>
对于上述有效负载,输出应为
<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”的所有Bvalue组成,并以“:”连接,然后对于每个业务对象,将其以“ |”分隔。 Bkey->使用bkey的直接映射Bvalue->使用Bvalue的直接映射BOID->必须为每个业务对象形成,需要将值的业务标识符的Bvalues连接为':'任何建议,我认为我都必须这里有两个复杂的循环,但无法破解。
谢谢
与查询
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.