【问题标题】:<xsl:sort cause error loading stylesheet: Parsing an XSLT stylesheet failed<xsl:sort 导致错误加载样式表:解析 XSLT 样式表失败
【发布时间】:2013-11-12 16:47:11
【问题描述】:

我试图弄清楚如何使用 XSL 按价格对目录的 XML 列表进行排序。现在它只显示来自 XML 的正确信息。如果我使用以下代码。

<xsl:template match="catalog">
    <html>
        <head>
            <title>book catalog</title>
        </head>
        <body>
            <h1>book catalogs</h1>
            <table border="1">
                <thead>
                    <tr bgcolor="red">
                        <td>id</td><td>author</td><td>title</td><td>generation</td><td>price</td><td>publish date</td><td>description</td>
                    </tr>
                </thead>
                <xsl:for-each select="book">
                    <xsl:sort data-type="number" select="price" />
                    <tbody>
                    <tr>

                        <td><xsl:value-of select="@id"/></td>
                        <td><xsl:value-of select="author"/></td>
                        <td><xsl:value-of select="title"/></td>
                        <td><xsl:value-of select="generation"/></td>
                        <td><xsl:value-of select="price"/></td>
                        <td><xsl:value-of select="publishDate"/></td>
                        <td><xsl:value-of select="description"/></td>
                    </tr>
                </tbody>
                </xsl:for-each>
            </table>
        </body>
    </html>



</xsl:template>

但如果我改变 xsl:sort 的位置并将其放在 tbody 标记之后,则会产生错误。 这两个代码有什么区别?是否有必要在 xsl:for-each 之后插入 xsl:sort 或其他任何导致问题的东西?

        <body>
            <h1>book catalogs</h1>
            <table border="1">
                <thead>
                    <tr bgcolor="red">
                        <td>id</td><td>author</td><td>title</td><td>generation</td><td>price</td><td>publish date</td><td>description</td>
                    </tr>
                </thead>
                <xsl:for-each select="book">
                    <tbody>

                        <tr>
                        <xsl:sort data-type="number" select="price" />

                        <td><xsl:value-of select="@id"/></td>
                        <td><xsl:value-of select="author"/></td>
                        <td><xsl:value-of select="title"/></td>
                        <td><xsl:value-of select="generation"/></td>
                        <td><xsl:value-of select="price"/></td>
                        <td><xsl:value-of select="publishDate"/></td>
                        <td><xsl:value-of select="description"/></td>
                    </tr>
                </tbody>
                </xsl:for-each>
            </table>
        </body>

【问题讨论】:

  • xsl:sort 必须作为 xsl:for-each 的第一个孩子出现。您希望通过将其放在其他地方来实现什么?
  • 感谢您的帮助。没什么特别的,只是我想知道有什么区别以及确切的问题是什么。非常感谢,您的评论对我有帮助。

标签: xml xslt


【解决方案1】:

根据规范

XSLT 1.0:

<!-- Category: instruction -->
<xsl:for-each
  select = node-set-expression>
  <!-- Content: (xsl:sort*, template) -->
</xsl:for-each>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:for-each
  select = expression>
  <!-- Content: (xsl:sort*, sequence-constructor) -->
</xsl:for-each>

xsl:sort 定义(如果存在)必须是 for-each 内的第一个子元素。

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2015-02-20
    • 2016-09-19
    相关资源
    最近更新 更多