views:

29

answers:

1

I want to append the values of some XML tags, and then append again the result. But I'm having some trouble appending the results, so I only have one final result of appending.

Imagine you have this:

<nodeA>
<nodeB>
 <item>1</item>
 <item>2</item>
 <yesno>blah</yesno>
</nodeB>
<nodeC>
 <thing>A</thing>
</nodeC>
</nodeA>

<nodeA>
<nodeB>
 <item>3</item>
 <item>4</item>
 <yesno>blah</yesno>
</nodeB>
<nodeC>
 <thing>B</thing>
</nodeC>
</nodeA>

I want to get this result <result>A,1,2,B,3,4</result> But I get this one: <result>A,1,2</result><result>B,3,4</result>

I've been reading this: http://stackoverflow.com/questions/1739479/xquery-how-to-properly-append-in-for-loop but still I can't make it work. Here is the sample of my code.

let $contador := count(
    for $x in $productDoc//*[substring(local-name(.),2,23)="nodeB"]
    where not(exists ($x/ns0:yesno))
    return
     $x)

 return
    if ($contador = 0) then
      <result> </result>
    else
      for $x in $productDoc//*[substring(local-name(.),2,18)="nodeB"]
      where not(exists ($x//ns0:yesno))
      return
          for $y in $x//*[matches(name(.),'thing')]
          let $z := $x//*[matches(name(.),'item')]
          return
            for $i in $y
            let $c := concat($i,",",string-join($z,","))
            return          
                let $d := string-join($c,",")
                <result>{ $d }</result>

I suppose what is happening is that I'm appending the partial results wrongly... or in a wrong place, but I don't know where to do it. Maybe I should call this function from another one that makes a string-join of the result. What do u think?

+1  A: 

The following query returns exactly what you need (running on example you provided):

<result>{
string-join(
  for $nodeA in $productDoc//nodeA
  return ($nodeA//thing, $nodeA//item), ",")
}</result>

Specify more details in your question if you need something more specific.

Shcheklein
Thank you so much. I addapted your query to my actual XML file and works correctly. I think I was making things more complicated than they were.
deb