XQuery/Displaying Lists

From Wikibooks, open books for an open world
< 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>x</tag>
   <tag>y</tag>
   <tag>z</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,x,c,z</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.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export