Sergio
Sergio

Reputation: 197

XSLT:Insert multiple values in a table data

I'm trying to insert two or more values of element "inferente" in a td. This is usually easy, the problem is that this is in a for-each loop and i can't resolve the problem. The XML document is the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gruppo>
    <nome>Casa Miles</nome>
    <studente>
        <id>sergio</id>
        <nome>sergio</nome>
        <cognome>zavota</cognome>
        <scontrino>
            <prodotto>
                <nome>sapone piatti</nome>
                <quantità>1</quantità>
                <costo>3.3</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
                <inferente>
                    <id>sergio</id>
                </inferente>
            </prodotto>
            <prodotto>
                <nome>bresaola</nome>
                <quantità>1</quantità>
                <costo>5.5</costo>
                <inferente>
                    <id>sergio</id>
                </inferente>
            </prodotto>
            <prodotto>
                <nome>pasta</nome>
                <quantità>10</quantità>
                <costo>0.5</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
                <inferente>
                    <id>sergio</id>
                </inferente>
            </prodotto>
            <prodotto>
                <nome>pane</nome>
                <quantità>3</quantità>
                <costo>1.4</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
                <inferente>
                    <id>sergio</id>
                </inferente>
            </prodotto>
            <data>2020-02-03</data>
        </scontrino>
        <pagamenti>
            <data>2020-02-03</data>
            <inferente>
                <id>Stefano</id>
                <quota>-33.0</quota>
            </inferente>
        </pagamenti>
    </studente>
    <studente>
        <id>stefano</id>
        <nome>stefano</nome>
        <cognome>Silvestri</cognome>
        <scontrino>
            <prodotto>
                <nome>shampoo</nome>
                <quantità>2</quantità>
                <costo>2.3</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
            </prodotto>
            <prodotto>
                <nome>insalata</nome>
                <quantità>4</quantità>
                <costo>0.5</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
                <inferente>
                    <id>sergio</id>
                </inferente>
            </prodotto>
            <prodotto>
                <nome>hamburger</nome>
                <quantità>1</quantità>
                <costo>3.6</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
            </prodotto>
            <prodotto>
                <nome>pane</nome>
                <quantità>3</quantità>
                <costo>1.4</costo>
                <inferente>
                    <id>stefano</id>
                </inferente>
                <inferente>
                    <id>sergio</id>
                </inferente>
            </prodotto>
            <data>2020-03-03</data>
        </scontrino>
        <pagamenti>
            <data>2020-03-03</data>
            <inferente>
                <id>Sergio</id>
                <quota>33.0</quota>
            </inferente>
        </pagamenti>
    </studente>
</gruppo>

Like you see, there are sometimes two elements of "inferente" in a "prodotto" element. The XSLT is the following:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:strip-space elements="yes"/>
<xsl:key name="tableByDataScontrino" match="scontrino" use="data" />

<xsl:template match="/">
  <html>
   <head>
      <title>HTML Document</title>
   </head>

       <style>
            table {
              font-family: arial, sans-serif;
              border-collapse: collapse;
              width: 100%;
            }

            td, th {
              border: 1px solid #dddddd;
              text-align: left;
              padding: 8px;
            }

            tr:nth-child(even) {
                background-color: #dddddd;
            }   

            caption {
              display: table-caption;
              text-align: center;
            }

        </style>

   <body>

    <h3>Benvenuto <xsl:value-of select="gruppo/studente/nome"/></h3>
    <h3>Gruppo: <xsl:value-of select="gruppo/nome"/> </h3>
    <h3>Scontrini</h3>

    <xsl:for-each select="gruppo/studente/scontrino[generate-id() = generate-id(key('tableByDataScontrino',data)[1])]">
        <table>
          <caption style="font-weight: bold;">Data: <xsl:value-of select="data"/></caption>

          <tr>
            <th>Nome</th> 
            <th>Quantità</th>
            <th>Costo</th>
            <th>Totale</th>
            <th>Inferenti</th>      
          </tr>
          <xsl:for-each select="key('tableByDataScontrino',data)/prodotto">
            <xsl:sort select="data" /> 
            <tr>
              <td><xsl:value-of select="nome"/></td>
              <td><xsl:value-of select="quantità"/></td>
              <td><xsl:value-of select="costo"/></td>
              <td>Calcolato tramite Javascript</td>
              <td>
                <xsl:for-each select="prodotto">
                    <xsl:value-of select="inferente"/>
                </xsl:for-each>
              </td>   
            </tr>

          </xsl:for-each>
        </table>
      </xsl:for-each>
   </body>
  </html>
</xsl:template>

</xsl:stylesheet>

If i don't use the for-each statement inside the table data td, in the cell will be printed only the first "inferente" element of two. Thanks in advance.

Upvotes: 0

Views: 488

Answers (2)

Sergio
Sergio

Reputation: 197

So the solution is to change:

      <td>
        <xsl:for-each select="prodotto">
            <xsl:value-of select="inferente"/>
        </xsl:for-each>
      </td>

to

      <td>
        <xsl:for-each select="inferente">
            <xsl:value-of select="."/>
        </xsl:for-each>
      </td>

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167696

Simply change

         <td>
            <xsl:for-each select="prodotto">
                <xsl:value-of select="inferente"/>
            </xsl:for-each>
          </td>  

to

         <td>
            <xsl:for-each select="prodotto/inferente">
                <xsl:value-of select="."/>
            </xsl:for-each>
          </td>  

Upvotes: 1

Related Questions