1
votes

I am trying to figure out how to sort the XML list of catalog numerical by price using XSL. Right now it just displays the correct information from XML. If I use the following code.

<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>

but if i change the place of xsl:sort and place it after tbody tag, it will generate error. what is difference between this two codes? is it necessary to insert xsl:sort after xsl:for-each or any thing else cause the problem?

        <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>
1
xsl:sort must come as the first child of xsl:for-each. What were you hoping to achieve by putting it elsewhere?Michael Kay
thanks for helping. nothing special, only i wanted to know what is difference and what is the exact problem. thank you at all, your comment helped me.Keller123

1 Answers

1
votes

According to the specification

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>

the xsl:sort definition(s), if present, must be the first child element(s) inside the for-each.