robots for exide using FLWOR

46 Views Asked by At

-R (CARGAS PEQUEÑAS-MEDIANAS)

--RS080N

--RS003N

--RS010L

-CX (GRANDES CARGAS)

--CX110L

--CX210L

I need this result (an ul inside an ul) using this xml

<robots>
    <serie>
        <codigo>R</codigo>
        <descripcion>CARGAS PEQUEÑAS-MEDIANAS</descripcion>
    </serie>
    <serie>
        <codigo>CX</codigo>
        <descripcion>GRANDES CARGAS</descripcion>
    </serie>
    <robot>
        <codigo serie="R">RS003N</codigo>
    </robot>
    <robot>
        <codigo serie="R">RS010L</codigo>
    </robot>
    <robot>
        <codigo serie="R">RS080N</codigo>
    </robot>
    <robot>
        <codigo serie="CX">CX110L</codigo>
    </robot>
    <robot>
        <codigo serie="CX">CX210L</codigo>
    </robot>
</robots>

I tried this

       <section>
            {for $i in doc("/db/exam/robots.xml")/robots/serie
                return 
            <ul>
                <li> <h3>Serie: {$i/codigo/text()} ({$i/descripcion/text()})</h3> </li>
                <li>
                    <ul>
                        {for $h in $i/../robot
                        return <li> <h3>{$h/codigo/text()}</h3> </li>}
                    </ul>
                </li>
            </ul>
            }
       </section>

But it doesn't work. Any ideas?

3

There are 3 best solutions below

0
On BEST ANSWER
<li>
  <ul>
    {for $h in $i/../robot
    where $h/codigo/@serie=$i/codigo
    return <li> <h3>{$h/codigo/text()}</h3> </li>}
 </ul>
</li>

0
On

Try it this way and see if it works:

let $doc :=  """your xml above"""

for $i in $doc
let $codigo := $i//serie/codigo/text()
for $co in $codigo
return 
            <ul>
                <li> <h3>Serie: {$co} ({$co/../following-sibling::descripcion/text()})</h3> </li>
                <li>
                    <ul>
                        {for $h in $i/robot/codigo[@serie=$co]
                        return <li> <h3>{$h}</h3> </li>}
                    </ul>
                </li>
            </ul>  
0
On

It can be helpful to take a step back and think about this task conceptually.

From an abstract perspective, what you need to do is

  • split robots from series

  • group robots by serie

  • for each group, lookup the corresponding serie (=join)

Once you have done this, obtaining the final output comes down to two simple nested FLWOR expressions.

I hope this helps!