Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing values within and bewteen files.

Status
Not open for further replies.

frodosfoot

IS-IT--Management
Nov 29, 2006
9
0
0
GB
<rankings>

<entry>
<club>example1</club>
<oldrank>2000.34</oldrank>
<newrank>1973.29</newrank>
</entry>

<entry>
<club>example2</club>
<oldrank>2104.68</oldrank>
<newrank>2156.36</newrank>
</entry>

</rankings>


This simple xml file is curently transformed by an equally simple xslt stylesheet to produce a 3 column table, that orders he <clubs> based on their current ranking points total (from <newrank>). The table gives: rank position -- club -- rankpoints.

Is there an xsl method that would allow a fourth coulmn to be prodced that would display a graphical image ("up.png" or "down.png") that would indicate whether the <newrank> was higher or lower than the <oldrank>?

i.e. IF <newrank> is higher than <oldrank>, then output "up.png", BUT if <newrank> is lower than <oldrank> then output "down.png"


On a side note, I would be interested to know if such a feat could be accomplished by just comparing two separate xml files of the format:

new.xml:
<rankings>

<entry>
<club>example1</club>
<rank>2000.34</rank>
</entry>

</rankings>

old.xml:
<rankings>

<entry>
<club>example1</club>
<rank>2104.68</rank>
</entry>

</rankings>


Wherein, both files just contain a single <rank> value, with the transformed new.xml to display the relevant image. Sounds complicated, which is why I opted for the first option mentioned above.

Thanks for your time.

 
After some experimentaion I have managed to come up with the following:

<xsl:for-each select="ScotlandRanks/entry">
<xsl:sort select="newrank" data-type="number" order="descending"/>

<tr>
<td><xsl:number value="position()" format="1. "/></td>
<td><xsl:value-of select="team"/></td>
<td><xsl:value-of select="newrank"/></td>

<xsl:choose>
<xsl:when test="newrank - oldrank &gt; 0">
<td><img src="../graffix/up.gif" /></td>
</xsl:when>
<xsl:eek:therwise>
<td><img src="../graffix/down.gif" /></td>
</xsl:eek:therwise>
</xsl:choose>
</tr>
</xsl:for-each>


It seem to put the right image in the table as desired.



As for doing the same thing but between files, is that possible?
 
Yes, I should have realised this - I had something similar only a day or two ago.

From the example above, I find it rather odd that the xslt processor on my system is so shockingly poor at arithmetic.
When calculating the difference between <newrank> and <oldrank> it produces wrong answers.

For example, since when did 1873.32 - 1754.02 = 119.29999999999995? The actual number is 119.30 (no rounding required). Makes a nice messs of the table too.
 
Sorry for bothering you guys with my simple question recently - I've been thrown in at the deep end, and have had to muddle through at speed from a standing start. I think I've finished now - thanks for the time and the speedy help that I received. The "project" is coming along nicely now.


For the record, I fixed the math problem with:

<xsl:value-of select="format-number(number(newrank - oldrank),'####0.00')" />
 
Does it automagically round to the hundredths digit?

Also, might I suggest [tt]number(newrank) - number(oldrank)[/tt]? Seems a bit safer.

Your arithmetic problem is a well known defect of floating point arithmetic. Interestingly, the round() function only rounds to integer values!

Welcome to the ranks of XSLT experts!
guru.gif


Tom Morrison
 
Yes, it seems to be giving me the real values one would expect to see if the math was done in the head or on a calculator. For the above example it gives 119.30. And for 1754.22 - 1873.39 it gives -119.17. (I haven't used a full data set yet though).

I'll try the alternative you mentioned too.

Thank again.
 
Hey...just saw the Scotland reference in the xsl:for-each. Where in Scotland? Spent a very nice week there w/ family in June.
wr1083r.gif


Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top