浏览器上的XSLT处理器不执行转换 [英] XSLT Processor on browser doesn't perform the transformation

查看:11
本文介绍了浏览器上的XSLT处理器不执行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了在Internet上找到的XSLT处理器将一个XML文件转换为一个HTML文件。 它起作用了,但当我尝试在浏览器上做同样的事情,将引用<?xml-stylesheet type="text/xsl" href="contabilità.xsl"?>添加到XML文件中时,它不起作用。 我尝试了火狐、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,出于安全考虑,可能无法修复。

但您可以通过在Firefox中修改about:config来规避此问题。
设置

privacy.file_unique_origin = false

然后,您至少可以在Firefox中将XML与本地XSLT一起使用。
免责声明:我不知道Edge:-(

这篇关于浏览器上的XSLT处理器不执行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆