Jump to content

Sorting Xml String


palanikumark
 Share

Recommended Posts

How to sort the following xml string...? Based on num tag....

var text="<?xml version='1.0'?><solution><note>  <to>Tove</to>  <from>Ja</from>  <heading>Reminder</heading>  <body>Dont forget me this weekend!</body><num>2</num> </note> <note> <to>Tove</to>  <from>Mathi</from>  <heading>Reminder</heading>  <body>Dont forget me this weekend!</body><num>1</num> </note></solution>";

Link to comment
Share on other sites

It's terribly hard to do that in JavaScript.It's much easier in XSLT, if you know it, and can actually run it... in JavaScript, the only reliable cross browser way is with the Sarissa library.In XSLT, you can do it with something like:

<xsl:template match="solution">	<xsl:copy>		<xsl:apply-templates select="note">			<xsl:sort select="num" datatype="number" />		</xsl:apply-templates>	</xsl:copy></xsl:template><xsl:template match="/|node()">	<xsl:copy>		<xsl:apply-templates />	</xsl:copy></xsl:template>

Edited by boen_robot
Link to comment
Share on other sites

Thank you. I tried using xslt. But still I stuck with this sorting. Where I did mistake..? Here is my code. I'm using safari browser.

<html><head><script type="text/javascript">function parseXML(){text="<?xml version='1.0'?><solution><note>  <to>Tove</to>  <from>Jan</from>  <heading>Reminder</heading>  <body>Dont forget me this weekend!</body><num>2</num> </note> <note> <to>John</to>  <from>Trieste</from>  <heading>Reminder</heading>  <body>Dont forget me this weekend!</body><num>1</num> </note><note> <to>Bavan</to>  <from>London</from>  <heading>Reminder</heading>  <body>Dont forget me this weekend!</body><num>3</num> </note></solution>";try //Internet Explorer  {  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  xmlDoc.async="false";  xmlDoc.loadXML(text);  }catch(e)  {  try //Firefox, Mozilla, Opera, etc.  {  parser=new DOMParser();  xmlDoc=parser.parseFromString(text,"text/xml");  //alert(xmlDoc);  }  catch(e)  {  alert(e.message);  return;  }}	var temp = text.replace("<?xml version='1.0'?>", "<?xml version='1.0'?><?xml-stylesheet type='text/xsl' href='sortingstring.xsl'?>");		parser1=new DOMParser();		temp=parser1.parseFromString(temp,"text/xml");	for (var i=0; i<3;i++ )	{		alert(temp.getElementsByTagName("num")[i].childNodes[0].nodeValue);	}return xmlDoc;	}</script></head><body onload="parseXML()"></body></html>

Edited by kpkumar
Link to comment
Share on other sites

Using XSLT with JavaScript is not as easy as just putting the processing instruction in the XML. You need to actually call upon the XSLT processor. You do that in different way across browsers, which is why the above mentioned Sarissa library exists - it provides you with a few functions that can launch an XSLT transformation in any browser. Try using that.

Link to comment
Share on other sites

Using XSLT with JavaScript is not as easy as just putting the processing instruction in the XML. You need to actually call upon the XSLT processor. You do that in different way across browsers, which is why the above mentioned Sarissa library exists - it provides you with a few functions that can launch an XSLT transformation in any browser. Try using that.
Thank you boen_robot for your quick reply. Actually I dont know about xslt and sarissa. So please can you give me the small example for xml string sorting. It will be greatly helpful for me...
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...