【发布时间】:2020-06-27 19:00:24
【问题描述】:
我使用 Internet 上的 XSLT 处理器将 XML 文件转换为 HTML 文件。
它起作用了,但是当我尝试在浏览器上做同样的事情时,将引用 <?xml-stylesheet type="text/xsl" href="contabilità.xsl"?> 添加到 XML 文件中,它不起作用。
我尝试使用 Firefox、Chrome 和 Edge,结果是一样的。
这是 XML 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="contabilità.xsl"?>
<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>
<partecipante>
<id>stefano</id>
</partecipante>
<partecipante>
<id>sergio</id>
</partecipante>
</prodotto>
<prodotto>
<nome>bresaola</nome>
<quantità>1</quantità>
<costo>5.5</costo>
<partecipante>
<id>sergio</id>
</partecipante>
</prodotto>
<prodotto>
<nome>pasta</nome>
<quantità>10</quantità>
<costo>0.5</costo>
<partecipante>
<id>stefano</id>
</partecipante>
<partecipante>
<id>sergio</id>
</partecipante>
</prodotto>
<prodotto>
<nome>pane</nome>
<quantità>3</quantità>
<costo>1.4</costo>
<partecipante>
<id>stefano</id>
</partecipante>
<partecipante>
<id>sergio</id>
</partecipante>
</prodotto>
<data>2020-02-10</data>
<pagato>true</pagato>
</scontrino>
<pagamenti>
<partecipante>
<id>Stefano</id>
<quota>-33.0</quota>
</partecipante>
</pagamenti>
</studente>
<studente>
<id>stefano</id>
<nome>stefano</nome>
<cognome>Silvestri</cognome>
<scontrino>
<prodotto>
<nome>shampoo</nome>
<quantità>2</quantità>
<costo>2.3</costo>
<partecipante>
<id>stefano</id>
</partecipante>
</prodotto>
<prodotto>
<nome>insalata</nome>
<quantità>4</quantità>
<costo>0.5</costo>
<partecipante>
<id>stefano</id>
</partecipante>
<partecipante>
<id>sergio</id>
</partecipante>
</prodotto>
<prodotto>
<nome>hamburger</nome>
<quantità>1</quantità>
<costo>3.6</costo>
<partecipante>
<id>stefano</id>
</partecipante>
</prodotto>
<prodotto>
<nome>pane</nome>
<quantità>3</quantità>
<costo>1.4</costo>
<partecipante>
<id>stefano</id>
</partecipante>
<partecipante>
<id>sergio</id>
</partecipante>
</prodotto>
<data>2020-03-10</data>
<pagato>true</pagato>
</scontrino>
<pagamenti>
<partecipante>
<id>Sergio</id>
<quota>33.0</quota>
</partecipante>
</pagamenti>
</studente>
</gruppo>
这是 XSL 文件:
<?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 onload="getCurrentData(); getProductPrize()">
<h2 align="center">Benvenuto <xsl:value-of select="gruppo/studente[nome='sergio']/nome"/></h2>
<h2 align="center">Gruppo: <xsl:value-of select="gruppo/nome"/> </h2>
<h2 align="center">Scontrini</h2>
<xsl:for-each select="gruppo/studente/scontrino[generate-id() = generate-id(key('tableByDataScontrino',data)[1])]">
<table>
<input type="checkbox" checked="checked">
<xsl:choose>
<xsl:when test="pagato = true">
<xsl:attribute name="value">1</xsl:attribute>
<xsl:attribute name="checked">unchecked</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="value">0</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</input>
<label for="pagato"> Pagato</label>
<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>Partecipanti</th>
</tr>
<xsl:for-each select="key('tableByDataScontrino',data)/prodotto">
<xsl:sort select="data" />
<tr class="prodotto">
<td><xsl:value-of select="nome"/></td>
<td class="quantità"><xsl:value-of select="quantità"/></td>
<td class="costo"><xsl:value-of select="costo"/></td>
<td class="prezzoTotale">Calcolato tramite Javascript</td>
<td>
<xsl:for-each select="partecipante">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
<h2 align="center" id="dataOdierna"></h2>
<table>
<tr>
<th>Studente</th>
<th>Quota</th>
</tr>
<tr>
<td><xsl:value-of select="gruppo/studente[nome='sergio']/pagamenti/partecipante/id"/></td>
<td><xsl:value-of select="gruppo/studente[nome='sergio']/pagamenti/partecipante/quota"/></td>
</tr>
</table>
<script src="contabilità.js"></script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
文件位于同一目录中。 我知道如果文件在本地,Chrome 会出错,但对于其他两个应该没有任何问题。
【问题讨论】:
标签: html xml xslt transform processor