【发布时间】: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