|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
finding max string length if more than 2 elements
suppose i have an XML file like the following,
<?xml version="1.0" encoding="UTF-8"?> <registers> <register> <name>ramesh</name> <Bits><start>15</start></Bits> </register> <register> <name>xxxxxxxx</name> <Bits><start>12</start></Bits> </register> <register> <name>srinivas</name> <Bits><start>20</start><end>16</end></Bits> </register> <register> <name>rajagopi</name> <Bits><start>6</start><end>0</end></Bits> </register> <register> <name>rama</name> <Bits><start>10</start><end>7</end></Bits> </register> </registers> here i have to find maximum string length. if i use the xsl file like the following i am getting the result as srinivas. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:variable name="Length"> <xsl:for-each select="/employees/emp/name"> <xsl:sort select="string-length(.)" order="descending" data-type="number"/> <xsl:if test="position() = 1"> <xsl:value-of select="string-length(.)"/> </xsl:if> </xsl:for-each> </xsl:variable> Max string is: <xsl:value-of select="$Length"/> </xsl:template> </xsl:stylesheet> but here we have 3 registers( xxxxxxxx,srinivas,rajagopi) with max string length of 8. here if i find the range of bits, like, start-end, for xxxxxxxx it is 1, because there is no end, and for srinivas it is 4(20-16) and for rajagopi it is 6(6-0). so in these kind of situations i have to give preference to xxxxxxxx, i.e max string length with range 1. so if i get 2 or more strings with same string length and with various ranges, then first 1. i have to get the string with max string length and range is 1. otherwise if max string length, strings with range 1 are not there, then 2. i should get the string with max string length and range is max. for the second condition suppose i have srinivas and rajagopi with ranges 4 and 6, so i have to get rajagopi. is it possible to get it? how i have to change my xsl file so that i will get this result? please help me . i am trying for it since 3 days. thanks, srini. srini |
|
#2
|
|||
|
|||
|
This one really got me confused, the XSL you posted doesn't match the XML? And the xxxxxxxx element has a rank of 12, not 1
Anyway here's my solution, if it doesn't match your XML then I'm pretty sure you'll get the idea Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:variable name="Length"> <xsl:for-each select="/registers/register/name"> <xsl:sort select="string-length(.)" order="descending" data-type="number"/> <xsl:if test="position() = 1"> <xsl:value-of select="string-length(.)"/> </xsl:if> </xsl:for-each> </xsl:variable> <xsl:variable name="HighestRankedName"> <xsl:for-each select="/registers/register[name[string-length()=$Length]]"> <xsl:sort select="Bits/start - Bits/end" order="descending" data-type="number"/> <xsl:if test="position() = 1"> <xsl:value-of select="name"/> </xsl:if> </xsl:for-each> </xsl:variable> Max string is: <xsl:value-of select="$Length"/> <br/> Highest ranked name: <xsl:value-of select="$HighestRankedName"/> </xsl:template> </xsl:stylesheet> I'm not sure if you want position() = last() or position() =1 there, one selects highest rank and the other selects lowest Last edited by teedee : November 7th, 2003 at 06:20 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > finding max string length if more than 2 elements |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|