2

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.

1
  • Good question, +1. See my answer for a very short XPath one-liner solution. :) Commented Nov 23, 2010 at 14:22

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

+1 This is better answer (not ussing for). Consider to use string-join() because otherwise you get a sequence, so after the new-line, an space will be added.
@Alejandro: I am not going to output the result using <xsl:sequence>, so I am not concerned about the final appearance. You are right that there are many ways to get the concatenation of the three text nodes without any intervening delimiters. But you are right, anyway -- I have edited my answer with the proposed fix. BTW, Why did you delete your answer? Please, undelete it, I wanted to upvote it for being also correct and short enough.
Thanks, but ussing a fuction call as last step is better in a functional sense.
0

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

2 Comments

the xml is a column of a table in database. and if i use the xpath: /persons/person/name it will get only the first it finds right?
May I ask what you are using at the backend? Asp.NET? Java? PHP? As for your question - No, to my knowledge, It will return a set of matched nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.