Hi all,

I am trying to have a recursive sort on my xml data,
but can only manage to sort the first layer..and not as what i want too :( , been trying to do this for a couple of weeks :confused:

I would appreciate it if anyone could help me with this recursion thingy.. please... :)

my xml is like this:

<test>
	<char id='1'>
		<char id='3'/>
		<char id='1'/>
		<char id='2'/>
	</char>
	<char id='3'>
		<char id='2'/>
		<char id='1'/>
		<char id='3'/>
	</char>
	<char id='2'>
		<char id='1'>
			<char id='i'/>
			<char id='iv'/>
			<char id='ii'/>
			<char id='iii'/>
		</char>
		<char id='3'/>
		<char id='2'/>
	</char>	
</test>

and i want the output to look like this:

<test>
	<char id='1'>
		<char id='1'/>
		<char id='2'/>
		<char id='3'/>
	</char>
	<char id='2'>
		<char id='1'>
			<char id='i'/>
			<char id='ii'/>
			<char id='iii'/>
			<char id='iv'/>
		</char>
		<char id='2'/>
		<char id='3'/>
	</char>		
	<char id='3'>
		<char id='1'/>
		<char id='2'/>
		<char id='3'/>
	</char>
</test>

my xsl is like this:

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

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

	<xsl:output method="xml"/>	
	
	<xsl:template match="test ">
		<xsl:call-template name="sortchar" >
			<xsl:with-param name="listOfchar" select="char" />
		</xsl:call-template>	
	</xsl:template>		

	<xsl:template name="sortchar">
		<xsl:param name="listOfchar" />
		<xsl:variable name="firstchar" select="$listOfchar[1]" />
		<xsl:variable name="restOfchars" select="$listOfchar[position() != 1]" />

		<xsl:call-template name="sort">
			<xsl:with-param name="charsToSort" select="$firstchar/char" />
		</xsl:call-template>
	</xsl:template>

	<xsl:template name="sort" >
		<xsl:param name="charsToSort" />
		<xsl:if test="$charsToSort" >
			<xsl:call-template name="sortchar">
				<xsl:with-param name="listOfchar" select="$charsToSort/@id" />
			</xsl:call-template>
		</xsl:if>
		<xsl:apply-templates select="$charsToSort/@id" >
			<xsl:sort/>
		</xsl:apply-templates>		
	</xsl:template>

</xsl:stylesheet>

Thanking everyone in advance!

Hi,

i've got this sorted..
Thanks... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.