2

I want to make a table in which I'm going to make an unordered list with the names of the hotels and the number of stars like this:

Hoteles


· Macuya (4)


· Fuentevino (2)

· Tresarazos (3)


Using this code:

<vacaciones>
    <destino identificador="p023">
        <estancias>
          <hotel estrellas="4">Macuya</hotel>
        </estancias>
    </destino>

    <destino identificador="m036">
        <estancias>
          <hotel estrellas="2">Fuentevino</hotel>
          <hotel estrellas="3">Tresarazos</hotel>
        </estancias>
    </destino>
</vacaciones>

I tried this in eXide, but the names come together without spaces and the number of stars isn't shown:

<table>
  <tr>
    <th>Hoteles</th>
  </tr>
  {for $i in doc("/db/exercise/vacaciones.xml")//destino
  return
    <tr>
      <td>
        <ul>
          <li>
            {$i/estancias/hotel/text()} ({$i/estancias/hotel/@estrellas/text()})
          </li>
        </ul>
      </td>
    </tr>
  }
</table>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Lordpascal
  • 51
  • 1
  • 6

2 Answers2

2

I think you want to nest a further for .. return .. expression (and correct the attribute selection although I have simply used the || string concatenation operator):

    <table>
      <tr>
        <th>Hoteles</th>
      </tr>
      {
      for $i in //destino
      return
        <tr>
          <td>
            <ul>
            {
                for $hotel in $i/estancias/hotel
                return 
                    <li>
                    {
                        $hotel || '(' || $hotel/@estrellas || ')'
                    }
                    </li>
            }
            </ul>
          </td>
        </tr>
      }
    </table>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks! :D Now I wanna put the image of a star right after the number of stars, between the (). I have no idea what you did. – Lordpascal Mar 29 '20 at 20:32
  • Like this: `$hotel || '(' || string-join((1 to $hotel/@estrellas) ! '⭐') || ')'`? – Martin Honnen Mar 29 '20 at 20:45
  • I'm trying your code with an attribute now, and it doesn't show the names. Do you need me to send you the code? – Lordpascal Mar 29 '20 at 20:46
  • Add any code to your question. If you select an attribute node with e.g. `@estrellas` you can't select any `/text()` child as your orginal code tried, as attribute nodes don't have any child nodes, instead, if you want the plain value, use `@estrellas/data()` or `@estrellas/string()`. – Martin Honnen Mar 29 '20 at 20:51
  • Thanks for the code, but how can you make like we did above and make an unordered list with attributes? Using, for example, the code I'm gonna show you above. – Lordpascal Mar 29 '20 at 20:58
  • If you have different input data and can't adpat an existing answer to that data then it is better to ask a new, separate question where you detail both input, wanted output, current code, result or error you get. – Martin Honnen Mar 29 '20 at 21:13
0

You could also use this approach (incorporating @Martin Honnen`s great star rendering)

declare function local:render-destination ($desination as element(destino)) {
  <ul>{for-each($destination//hotel, local:render-hotel(?))}</ul>
};

declare function local:render-hotel ($hotel as element(hotel)) {
  <li>{
    ``[`{$hotel}` (`{local:render-stars($hotel/@estrellas/data())}`)`]``
    }</li>

};

declare function local:render-stars ($n as xs:integer) {
  string-join((1 to $n) ! '⭐')
};

<section>
  <header>Hoteles</header>
  {for-each(//destino, local:render-destination(?))}
</section>
line-o
  • 1,885
  • 3
  • 16
  • 33