XML Input as below:
<Global>
<ProductFood>
<foodName>Burger</foodName>
<foodName>Snack</foodName>
</ProductFood>
<ProductToy>
<Product ProductID="1">
<productName>Doll</productName>
<Color>Green</Color>
</Product>
<Product ProductID="2">
<productName>Ball</productName>
<Color>White</Color>
</Product>
</ProductToy>
</Global>
XSLT Code that i have is below:
<xsl:template match="//Products">
<html>
<body>
<Products>
<xsl:apply-templates select="ProductToy" >
<xsl:sort select="@name"/>
<xsl:apply-templates/>
</Products>
</body>
</html>
</xsl:template>
<xsl:template match="Product">
<xsl:element name="product">
<xsl:attribute name="name" select="ProductName/text()" />
<xsl:element name="productID">
<xsl:value-of select="@ProductID" />
</xsl:element>
</xsl:element>
</xsl:template>
i wanted the output to return product attribute name by ascending.but my sorting is not working as it still shows the product ball first then only the doll.please advise how to make it work.
Use:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/Global">
<html>
<body>
<Products>
<xsl:apply-templates select="//Product">
<xsl:sort select="productName"/>
</xsl:apply-templates>
</Products>
</body>
</html>
</xsl:template>
<xsl:template match="Product">
<product name="{productName}">
<productID>
<xsl:value-of select="@ProductID"/>
</productID>
</product>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<Products>
<product name="Ball">
<productID>2</productID>
</product>
<product name="Doll">
<productID>1</productID>
</product>
</Products>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.