Jump to content

XSLT sorting not working


srvadali

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...