Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2011, 04:45 AM   PM User | #1
adrianulima
New to the CF scene

 
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
adrianulima is an unknown quantity at this point
how to sort data without the "sort command"

Hello everybody, I have a problem to sort data in XML, when added to a variable of another XML.

It works like this:
I have two XML, the first has the name and the score of a person, and the second has the name and a extra score.

I've got to build a table of data, even with the extra added to the score.
What was I could not even sort my table by this new score.

Below is the XML:
acc.xml
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<acc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<person>
		<name>Beto Almeida</name>
		<score>15</score>
	</person>
	<person>
		<name>Adriano Fraporti</name>
		<score>5</score>
	</person>
	<person>
		<name>Caetano Paiva</name>
		<score>5</score>
	</person>
	<person>
		<name>Francisco Junior</name>
		<score>15</score>
	</person>
	<person>
		<name>Natalia Perez</name>
		<score>15</score>
	</person>
	<person>
		<name>Ivan Castrolebre</name>
		<score>30</score>
	</person>
</acc>
add.xml
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<person>
		<name>Beto Almeida</name>
		<score>4</score>
	</person>
	<person>
		<name>Adriano Fraporti</name>
		<score>22</score>
	</person>
	<person>
		<name>Caetano Paiva</name>
		<score>7</score>
	</person>
	<person>
		<name>Francisco Junior</name>
		<score>2</score>
	</person>
	<person>
		<name>Natalia Perez</name>
		<score>33</score>
	</person>
	<person>
		<name>Ivan Castrolebre</name>
		<score>1</score>
	</person>
</add>
Below is the XSL:
example.xsl
Code:
<?xml  version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="acc">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Untitled Document</title>
</head>
<body>

	<table width="100%"  bgcolor="#909090" border="0" align="left">
	  <tr>
		<td align="left" width="120"><strong>Name</strong></td>
		<td align="center" width="120" ><strong>Score</strong></td>
		
		<td align="center" width="120" ><strong>Add</strong></td>
		<td align="center" width="120" ><strong>New Score</strong></td>
	  </tr>
	</table>
	<xsl:for-each select="person">
	<xsl:sort select="score" order="descending" data-type="number"/>
		
		<xsl:variable name="varname" select="name"/>
		<xsl:variable name="varadd" select="document('add.xml')/add/person[name=$varname]/score"/>
		<xsl:variable name="varnewscore" select="score+$varadd"/>
		
		<table width="100%" bgcolor="#C0C0C0" border="0" align="left">
		  <tr>
			<td align="left" width="120"> <xsl:value-of select="name"/> </td>
			<td align="center" width="120" > <xsl:value-of select="score"/> </td>
			
			<td align="center" width="120" ><xsl:value-of select="$varadd"/></td>
			<td align="center" width="120" ><xsl:value-of select="$varnewscore"/></td>
		  </tr>
		</table>
	</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>
Take a Look

So, anyone know how to sort those values by the column "New Scores"?
thank you now, and sorry for bad english.
adrianulima is offline   Reply With Quote
Old 01-20-2011, 11:49 PM   PM User | #2
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,427
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
The way your XSLT transforms the document, it looks impossible. You're generating a whole new HTML table for each person, so you don't have one table, but seven. (Take a look at the output in DOM Inspector or Firebug.)

Even if you fix that, there's probably going to be other underlying issues to deal with...
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog
Alex Vincent is offline   Reply With Quote
Old 01-21-2011, 02:41 AM   PM User | #3
adrianulima
New to the CF scene

 
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
adrianulima is an unknown quantity at this point
thanks for your reply.

if the problem was to have a table for each for-each, this new code can solve:
example.xsl
Code:
<?xml  version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="acc">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Untitled Document</title>
</head>
<body>

	<table width="100%"  bgcolor="#909090" border="0" align="left">
	  <tr>
		<td align="left" width="120"><strong>Name</strong></td>
		<td align="center" width="120" ><strong>Score</strong></td>
		
		<td align="center" width="120" ><strong>Add</strong></td>
		<td align="center" width="120" ><strong>New Score</strong></td>
	  </tr>
	</table>
	
		<table width="100%" bgcolor="#C0C0C0" border="1" align="left">
		<xsl:for-each select="person">
		<xsl:sort select="score" order="descending" data-type="number"/>		
		<xsl:variable name="varname" select="name"/>
		<xsl:variable name="varadd" select="document('add.xml')/add/person[name=$varname]/score"/>
		<xsl:variable name="varnewscore" select="score+$varadd"/>
		  <tr>
			<td align="left" width="120"> <xsl:value-of select="name"/> </td>
			<td align="center" width="120" > <xsl:value-of select="score"/> </td>
			
			<td align="center" width="120" ><xsl:value-of select="$varadd"/></td>
			<td align="center" width="120" ><xsl:value-of select="$varnewscore"/></td>
		  </tr>
		</xsl:for-each>
		</table>


</body>
</html>
</xsl:template>
</xsl:stylesheet>
but now what? which is my next step?
Thank you for your attention ^ ^


EDIT:
I did \o/

so, doing what you said I used a javascript code to sort the html table values!

take a look now
thank your help, was very important to my project.

but, I have any more question, and maybe I should post at javascript forum.

Is possible select a default column without the user have to click?
here is the javascript code

thanks again

Last edited by adrianulima; 01-21-2011 at 08:07 AM..
adrianulima is offline   Reply With Quote
Reply

Bookmarks

Tags
sort, xml, xsl

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:53 AM.


Advertisement
Log in to turn off these ads.