medusa
medusa

Reputation: 541

How to loop in a xml structure with xquery?

the xml is like this:

<persons>
<person>
<name/>
<surname/>
</person>

<person index=1>
<name/>
<surname/>
</person>

<person index=2>
<name/>
<surname/>
</person>
...
</persons>

I need to build a view that shows all data of all persons.

name surname
name1 surname1

How can i do this loop in a select statement? It needs to be a view.

Upvotes: 2

Views: 744

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

Use:

string-join(/*/person/concat(name, ' ', surname), '&#xA;')

when this XPath expression is evaluated, against the following XML document:

<persons>
    <person index="1">
        <name>Alex</name>
        <surname>Brown</surname>
    </person>
    <person index="2">
        <name>Katie</name>
        <surname>Smith</surname>
    </person>
    <person index="3">
        <name>Julius</name>
        <surname>Caesar</surname>
    </person>
</persons>

the result is:

 Alex Brown
 Katie Smith
 Julius Caesar

Upvotes: 1

Robin Maben
Robin Maben

Reputation: 23094

Have you considered using xslt if you need a transformation?

If you need to xquery, to select these nodes then,

doc("file.xml")/persons/person/name | /persons/person/name

OR

doc("file.xml")//name |// surname     

i.e. Name, Surname occuring anywhere

Upvotes: 0

Related Questions