XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 7th, 2003, 05:38 AM
sri197 sri197 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: bangalore
Posts: 4 sri197 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old November 7th, 2003, 06:17 PM
teedee teedee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 42 teedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > finding max string length if more than 2 elements


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway