代码之家  ›  专栏  ›  技术社区  ›  wonea Ilya Smagin

上的XSLT因未知原因失败xsl:的值

  •  0
  • wonea Ilya Smagin  · 技术社区  · 15 年前

    我一直在尝试创建一个xslt模板,但它总是默默地失败,就像发生了异常但没有被捕获一样。右括号未写出,输出无效;

    <?xml version="1.0"?>
    <gallery>
        <item>
            <file>IMAGEHEADER1.jpg</file>
            <thelink>michaeljackson123.htm</thelink>
        </item>
        <item>
            <file>IMAGEHEADER2.jpg</file>
            <thelink>barrywhite456.htm</thelink>
        </item>
    </gallery>
    

    XSLT文件

        <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="/">
            <html>
                <body>
                    <table>
                        <tr>
                            <xsl:apply-templates />
                        </tr>
                    </table>
                </body>
            </html>
        </xsl:template>
        <xsl:template match="gallery">
            <xsl:for-each select="item">
                <xsl:value-of select="position()"/>
                <xsl:choose>
                    <xsl:when test="position() = 1">
                        <td rowspan="2" height="122" width="510">
                            <xsl:apply-templates select="." />
                        </td>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </xsl:template>
        <xsl:template match="item">
            <a style="display:block;width:520px;height:330px" id="categorylink">
                <xsl:attribute name="href">
                    <xsl:value-of select="thelink"/>
                </xsl:attribute>
                <xsl:apply-templates select="file" />
            </a>
        </xsl:template>
        <xsl:template match="file">
            <img alt="">
                <xsl:attribute name="src">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </img>
        </xsl:template>
    </xsl:stylesheet>
    

    输出无效,缺少结束选项卡。

    <html>
    <body>
    <tr>1<td rowspan="2" height="122" width="510"><a style="display:block;width:520px;height:330px" id="categorylink" href="michaeljackson123.htm"><img alt="" src="IMAGEHEADER1.jpg"></a></td>2</tr>
    </body>
    </html>
    

    我的预期产出是;

    <html>
        <body>
            <table>
                <tr>1
                    <td rowspan="2" height="122" width="510">
                        <a style="display:block;width:520px;height:330px" id="categorylink" href="michaeljackson123.htm">
                            <img alt="" src="IMAGEHEADER1.jpg"></img>
                        </a>
                    </td>2
                </tr>
            </table>
        </body>
    </html>
    

    3 回复  |  直到 15 年前
        1
  •  1
  •   user357812 user357812    15 年前

    这是因为HTML和XML序列化之间的差异:

    样式表默认为HTML序列化,因为根元素是 html . 在本例中,所有DTD声明的空元素都按假设的方式输出:

    <img alt="" src="IMAGEHEADER1.jpg">
    

    如果要进行XML序列化,应声明:

    <xsl:output method="xml"/>
    

    那么您的输出将是:

    <html>
        <body>
            <table>
                <tr>1
                    <td rowspan="2" height="122" width="510">
                        <a style="display:block;width:520px;height:330px" id="categorylink" href="michaeljackson123.htm">
                            <img alt="" src="IMAGEHEADER1.jpg" />
                        </a>
                    </td>2
                </tr>
            </table>
        </body>
    </html>
    
        2
  •  2
  •   Nick Jones    15 年前

    <xsl:output method="xml"/>
    

    我认为发生的事情是序列化方法处于自动模式,当它在默认(不是xhtml)名称空间中看到html元素时,它默认为html序列化,在该名称空间中不需要关闭空标记。

        3
  •  1
  •   flatline    15 年前

    如何查看输出?在HTML中不需要img元素上的结束标记,因此如果您在web浏览器中查看它,很多时候浏览器将显示与其文本输入稍有不同的内容。我至少在firebug/chrome调试器中注意到了这一点。