[英]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.