[英]How to output XML node with same name[multiple occurrences] using an XQuery
My XML Response Load appears as below
<entries>
<id>1</id>
<UseCountIds>100</UseCountIds>
<UseCountIds>200</UseCountIds>
<UseCountIds>300</UseCountIds>
</entries>
Here 'entries' is parent node which has child elements viz 'id', 'UseCountIds'. 这里的“条目”是具有子元素即“ id”,“ UseCountIds”的父节点。 Above example is for id=1 .
上面的示例适用于id = 1。 Likewise , there are many 'entries' for id=2 or id=3 etc.
同样,id = 2或id = 3等的“条目”很多。
'id' is kind of unique value. “ id”是一种独特的价值。
I have done an XQuery to extract Child elements from XML Payload . 我已经做了一个XQuery来从XML Payload中提取Child元素。 Below is the Xquery used
以下是使用的Xquery
let $entries := /root/entries
return
for $entry in $entries
return
<entries>
{
$entry/id,
<UseCountIds>{data($entry/UseCountIds)}</UseCountIds>
}
</entries>
Problem is , with above XQuery output if it's load into .csv file, It is appearing as 问题是,如果将上面的XQuery输出加载到.csv文件中,它会显示为
id,UseCountIds
1,100 200 300
UseCountIds (Multiple values) are appearing in a column with space delimited. UseCountIds(多个值)出现在以空格分隔的列中。
My requirement is to have desired output like below 我的要求是要有如下所示的期望输出
id,UseCountIds
1,100
1,200
1,300
Also, UseCountIds are not limited to only 3 occurrences. 另外,UseCountIds不限于仅出现3次。 For a unique 'id' can have 'n' no.
对于唯一的“ id”可以为“ n”。 of UseCountIds .
的UseCountIds。
It is always good to bring UseCountIds connected to a unique 'id' in row level. 最好将UseCountIds连接到行级别的唯一“ id”。
Please share your thoughts how XQuery can be tweaked to get desired output mentioned above. 请分享您的想法,如何调整XQuery以获得上述所需的输出。
Thanks, TG 谢谢,TG
It isn't clear how you generate CSV from the output of the XQuery, but, if I can assume that you simply need one entries
element for each UseCountIds
in the source XML for it to produce the desired CSV data, then this is one possible XQuery : 目前尚不清楚如何从XQuery的输出中生成CSV,但是,如果我可以假设您只需要为源XML中的每个
UseCountIds
一个entries
元素来生成所需的CSV数据,那么这是一种可能XQuery:
for $c in /root/entries/UseCountIds
return
<entries>
{
$c/preceding-sibling::id,
$c
}
</entries>
If didn't produce the desired output, then please post how the XQuery output should be. 如果未产生所需的输出,则请发布XQuery输出应如何。
To be able to return entries
even when there is no child UseCountIds
: 为了即使没有子
UseCountIds
也能返回entries
:
for $e in /root/entries
let $id := $e/id
let $useCountIds := $e/UseCountIds
return
if($useCountIds) then
for $c in $useCountIds
return
<entries>
{
$id,
$c
}
</entries>
else
<entries>
{
$id,
<UseCountIds></UseCountIds>
}
</entries>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.