By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,846 Members | 2,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,846 IT Pros & Developers. It's quick & easy.

Getting the longest string

P: n/a
XML

<data>
<option>this is test 1</option>
<option>this is test 11</option>
<option>this is test 111</option>
<option>this is test 1111</option>
<option>this is test 11111</option>
</data>

The 5 option has the longest content. I need to get that length and place
in a variable. The test has to look across all of the option nodes and
return the longest one.

Any suggestions?

Cheers
Keith
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Keith Chadwick wrote:
<data>
<option>this is test 1</option>
<option>this is test 11</option>
<option>this is test 111</option>
<option>this is test 1111</option>
<option>this is test 11111</option>
</data>

The 5 option has the longest content. I need to get that length and place
in a variable. The test has to look across all of the option nodes and
return the longest one.


General solution is recursive template, but here is another trick:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="data">
<xsl:variable name="longest">
<xsl:for-each select="option">
<xsl:sort data-type="number" select="string-length()"
order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
Longest string is '<xsl:value-of select="$longest"/>'
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #2

P: n/a
Thanks Oleg, I ended up doing something very similar. Only difference was I
added the string length as an attribute to the node via the sql xml auto
then did 'exactly' what you stated below.

Quick question, I am using this length as a means of padding the string with
&nbsp; in order to create a table like structure in a drop down list with a
fixed width font, 'courier new'. In order to do this I used a msxml:script
with some JScript to build the string. Basically it is a padding function.
I could not find a way to do this in xslt but is there one? And is there
anything to watch out for when using server side JScript in a
transformation? For instance is there any specific performance hits?

Cheers
Keith Chadwick

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O4****************@TK2MSFTNGP09.phx.gbl...
Keith Chadwick wrote:
<data>
<option>this is test 1</option>
<option>this is test 11</option>
<option>this is test 111</option>
<option>this is test 1111</option>
<option>this is test 11111</option>
</data>

The 5 option has the longest content. I need to get that length and place in a variable. The test has to look across all of the option nodes and
return the longest one.


General solution is recursive template, but here is another trick:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="data">
<xsl:variable name="longest">
<xsl:for-each select="option">
<xsl:sort data-type="number" select="string-length()"
order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
Longest string is '<xsl:value-of select="$longest"/>'
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.