Pianta carnivora

XSL sorting and each

I have two files:

XML

<?xml version="1.0" encoding="UTF-8"?>

<Store>
    <Plant id="10">
        <Common>Pianta carnivora</Common>
        <Botanical>Dionaea muscipula</Botanical>
        <Quantity>10</Quantity>  
    </Plant>
    <Flower id="3">
        <Common>Fiore di prova</Common>
        <Quantity>999</Quantity>
    </Flower>
    <Plant id="20">
        <Common>Canapa</Common>
        <Botanical>Cannabis</Botanical>
        <Quantity>2</Quantity>  
    </Plant>    

    <Plant id="30">
        <Common>Loto</Common>
        <Botanical>Nelumbo Adans</Botanical>
        <Quantity>3</Quantity>  
    </Plant>    

</Store>

      

XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <xsl:apply-templates/>
        </html>
    </xsl:template>

    <xsl:template match="Store">
        <body>
            <xsl:for-each select="Plant">
                <p>
<xsl:sort select="Quantity"/>   
<xsl:value-of select="Quantity"/>

                </p>
            </xsl:for-each>
        </body>
    </xsl:template>


</xsl:stylesheet>

      

XSL doesn't sort. I have no choice. I really don't know how it doesn't work. The code seems to be correct. If you uncheck the sort tag, you will see the result. You will not see anything within the sort.

+3


source to share


1 answer


You need to move yours xsl:sort

as the first child xsl:for-each

. It is invalid where it is now.

You can also change yours data-type

to number

.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <xsl:apply-templates/>
        </html>
    </xsl:template>

    <xsl:template match="Store">
        <body>
            <xsl:for-each select="Plant">
                <xsl:sort select="Quantity" data-type="number"/>
                <p>
                    <xsl:value-of select="Quantity"/>
                </p>
            </xsl:for-each>
        </body>
    </xsl:template>

</xsl:stylesheet>

      



You can do the same with xsl:apply-templates

too ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/Store">
        <html>
            <body>
                <xsl:apply-templates select="Plant/Quantity">
                    <xsl:sort data-type="number"/>
                </xsl:apply-templates>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="Quantity">
        <p><xsl:value-of select="."/></p>
    </xsl:template>

</xsl:stylesheet>

      

+1


source







All Articles