srvadali Posted June 17, 2011 Posted June 17, 2011 HiI am trying to sort xml using xslt. But some how xslt is not sorting the xml. Here is my xslt =========== <?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="RES"> <xsl:copy> <xsl:apply-templates> <xsl:sort select="/GSP/RES/R/T"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="R"> <xsl:copy> <xsl:apply-templates> <xsl:sort select="T"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="@* | text()"> <xsl:copy/> </xsl:template></xsl:stylesheet> Here is my xml =========== <?xml version="1.0" encoding="UTF-8"?><GSP VER="3.2"> <RES SN="1" EN="10"> <R> <T>3</T> <seeta>test</seeta> </R> <R> <T>2</T> <seeta>test</seeta> </R> <R> <T>1</T> <seeta>test</seeta> </R> <R> <T>4</T> <seeta>test</seeta> </R> </RES></GSP> Expected result is ============= <?xml version="1.0" encoding="UTF-8"?><GSP VER="3.2"> <RES SN="1" EN="10"> <R> <T>1</T> <seeta>test</seeta> </R> <R> <T>2</T> <seeta>test</seeta> </R> <R> <T>3</T> <seeta>test</seeta> </R> <R> <T>4</T> <seeta>test</seeta> </R> </RES></GSP>
Martin Honnen Posted June 17, 2011 Posted June 17, 2011 I would do it like this: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="RES"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="R"> <xsl:sort select="T" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet>
srvadali Posted June 22, 2011 Author Posted June 22, 2011 Thanks a lot Martin... this is working fine... Where can I get good tutorial regarding xslt. Xslt is very powerful according to me... To become master in xslt can u through some tips. Regards,Seeta I would do it like this:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="RES"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="R"> <xsl:sort select="T" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet>
Martin Honnen Posted June 22, 2011 Posted June 22, 2011 For beginners books like http://www.amazon.com/Beginning-XSLT-Jeni-...9515&sr=8-1 are often recommended.Also Dave Pawson has collected http://www.dpawson.co.uk/xsl/sect2/sect21.html where you can check questions and answers.
srvadali Posted June 30, 2011 Author Posted June 30, 2011 Thanks Martin.... For beginners books like http://www.amazon.com/Beginning-XSLT-Jeni-...9515&sr=8-1 are often recommended.Also Dave Pawson has collected http://www.dpawson.co.uk/xsl/sect2/sect21.html where you can check questions and answers.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.