XQuery/Displaying Lists

From Wikibooks, the open-content textbooks collection

< XQuery
Jump to: navigation, search

Contents

[edit] Motivation

You have a list of items in an XML structure and you want to display a comma separated list of the values in an output string.

[edit] Method

XQuery provides the string-join() function that will take a sequence of items and a separator string and create and output string with the separator between each of the items. The format of the function is: string-join(nodeset, separator) where nodeset is a list of nodes and separator the string that you would like to separate the values with.

[edit] Sample Program

xquery version "1.0";

let $tags :=
<tags>
   <tag>a</tag>
   <tag>b</tag>
   <tag>c</tag>
   <tag>d</tag>
</tags>

return
<results>
   <comma-separated-values>{
      string-join($tags/tag, ',')
  }</comma-separated-values>
</results>

[edit] Output

<results>
   <comma-separated-values>a,b,c,d</comma-separated-values>
</results>


execute

[edit] Discussion

The string-join function takes two arguments, the first is the sequence of strings to be joined and the second is the separator.