简体   繁体   English

如何使用XQuery输出具有相同名称的XML节点[多次出现]

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

demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用XQuery输出具有相同名称[多次出现]但值不同的XML节点 - How to output XML node with same name[multiple occurrences] but different values using an XQuery XML - 使用 XSLT 合并同一节点多次出现的子元素 - XML - Merging child elements of multiple occurrences of the same node using XSLT 如何计算同名元素? XML-&gt; Xquery - How to count elements with same name? XML -> Xquery 如何使用 Python 从具有相同名称的 XML 输出多个元素? - How to output Multiple elements from an XML with the same name using Python? 如何使用XQuery计算XML中节点的出现次数? - How to count occurences of a node in XML using XQuery? 如何通过xQuery从XML中同名多个元素检索元素的值? - How To retrieve value of an element from same name multiple elements in XML through xQuery? 使用 XQuery 更新 XML 节点 - Update XML node using XQuery 如何遍历 xml 文件中的每个节点并在节点名称与字符串匹配时返回 true,否则使用 XQuery 和 XPath 返回 false? - How to go through each node in a xml file and return true if the name of the node matches a string and false otherwise using XQuery and XPath? 如何使用XQuery计数属性的出现 - How to count occurrences of attributes using XQuery 如何使用C#在xml文件的子节点下读取具有相同名称的多个标签? - How to read multiple tags with same name under child node in a xml file using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM