finding largest sting length in xml nodes using XSLT

Here should go questions about transforming XML with XSLT and FOP.
bharath_pepala
Posts: 1
Joined: Mon Mar 28, 2005 9:34 pm

finding largest sting length in xml nodes using XSLT

Post by bharath_pepala »

hi friends,
I have the following xml file.

<?xml version='1.0' encoding='UTF-8'?>

<lifesequencechart>
<table rows="15">
<lifeline id="1">obj1</lifeline>
<lifeline id="2">obj2222222</lifeline>
<lifeline id="3">obj3332</lifeline>
<lifeline id="4">obj4444</lifeline>
</table>

</lifesequencechart>

My question is how to find out the largest string length of <lifeline> using XSLT.
Can anyone help me how to find out this string length??

thanks,
Bharath
xacobea
Posts: 5
Joined: Wed Jan 05, 2005 1:07 pm

Post by xacobea »

do you search something like list?...

<xsl:apply-templates select="lifeline">
<xsl:sort select="string-length()" order="descending" data-type="number" />
</xsl:apply-templates>

...or only one single number? then you have to create a new xml (as far i don't know how one can copy a part of your old one and e.g. sort another part - maybe someone can help?):

<xsl:template match="/lifesequencechart">
<xsl:element name="root">
<xsl:apply-templates select="table/lifeline">
<xsl:sort select="string-length()" order="descending" data-type="number" />
</xsl:apply-templates>
</xsl:element>
</xsl:template>

<xsl:template match="lifeline">
<xsl:element name="newLifeline">
<xsl:value-of select="string-length()" />
</xsl:element>
</xsl:template>

and now you use the resulting xml and the following xsl:

<xsl:template match="/root">
<xsl:value-of select="lifeline[1]" />
</xsl:template>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Bharath,

Code: Select all


    <xsl:template match="/">
<xsl:value-of select="string-length(
lifesequencechart/table/lifeline/text() [
string-length(.) > string-length(../following-sibling::lifeline/text())
]
)"/>
</xsl:template>
Should print the max length.

Best Regards,
George
Post Reply