我使用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会给出错误消息,但对于其他两个文件应该没有任何问题。
您的XML和XSLT文件都正确。您已经注意到,Chrome无法正常运行。对于Firefox,此行为已注册as a bug,出于安全方面的考虑,该行为可能无法修复。
<?xml-stylesheet type="text/xsl" href="contabilità.xsl"?>
将通过HTTP(S)工作,而不是通过文件系统工作。这基本上是由于浏览器强加了安全性限制,有些是像很久以前的Chrome一样,有些是像Mozilla才是最近才发现的漏洞,因为该领域中的漏洞修复程序忽略了文件系统中XSLT的使用,或者如果使用基于Chromium的新版本则忽略了Edge。 Edge随着浏览器引擎的改变。