【问题标题】:XSLT:Insert multiple values in a table dataXSLT:在表数据中插入多个值
【发布时间】:2020-06-22 14:29:50
【问题描述】:

我正在尝试在 td 中插入两个或多个元素“inferente”的值。这通常很容易,问题是这是在 for-each 循环中,我无法解决问题。 XML 文档如下:

<?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>

如您所见,有时“prodotto”元素中有两个“inferente”元素。 XSLT 如下:

<?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>

如果我不在表数据 td 中使用 for-each 语句,则单元格中将仅打印两个中的第一个“inferente”元素。 提前致谢。

【问题讨论】:

    标签: html xml xslt foreach grouping


    【解决方案1】:

    简单的改变

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

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

    【讨论】:

    • 我试图这样做,但结果“推断”列的单元格是空的。但我试图改变: 并且它有效:) 谢谢
    【解决方案2】:

    所以解决办法是改变:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多