• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding max string length if more than 2 elements are having same length.

 
Srinivas Rallapalli
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
<xsl utput 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.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<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>

I was trying to follow this and wasn't sure of your requirement....
you seems to have afor loop, then you are ordering
them and picking the length of the first one, right ?
- madhav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic