0

I have a requirement where an XML element node say 'UseCountIds' having multiple occurrences and with multiple values . As element node name remain same, I need to distinguish each of these as UseCountIds1,UseCountIds2,UseCountIds3.

As of now, it is equipped to have only 3 occurrences.

Response payload appears as below . On which Xquery to be executed.

           <entries>
                <id>1</id>
                <UseCountIds>100</UseCountIds>
                <UseCountIds>200</UseCountIds>
                <UseCountIds>300</UseCountIds>
          </entries>

Using an Xquery , it is expected to output to delimited file.

        Output File
         id,UseCountIds1,UseCountIds2,UseCountIds3
         1,100,200,300

Expecting your valuable thoughts.

Tom George
  • 29
  • 1
  • 1
  • 7
  • Basically, looking to extract same named element nodes as unique ones. Also, if no data is available for all nodes or any, let it output as blank or default values. – Tom George Jul 19 '16 at 00:55

1 Answers1

1

If the tag structure of your XML input is fixed and only the values are variable, then the simplest query would be

concat(
  "id,UseCountIds1,UseCountIds2,UseCountIds3&#xa;",
  string-join(/entries/*, ',')
) 

A more generic solution that works with any child tags might be

declare function f:label($element as element()) as xs:string {
  if (count($element/../*[node-name(.)=node-name($element)]) gt 1)
  then concat(local-name($element), count($element/preceding-sibling::*[node-name(.)=node-name($element)]))
  else local-name($element)
};

concat(string-join(/entries/*/f:label(.), ','), '&#xa;',
string-join(/entries/*, ',')
Michael Kay
  • 156,231
  • 11
  • 92
  • 164