To be more specific, you need to create a key:
<xsl:key name="elementsByName" match="/*/*" use="local-name()" />
and then use it to select just one element of each name-group:
<!-- select all elements that are the first of their name-group -->
<xsl:for-each select="/*/*[generate-id() =
generate-id(key('elementsByName',
local-name())[1])]">
<!-- do a sorted for-each on elements of the current name-group -->
<xsl:for-each select="/*/*[local-name() = local-name(current())]">
<xsl:sort data-type="number" order="descending"
select="string-length(.)" />
<xsl:if test="position() = 1" />
<xsl:value-of select="concat(local-name(), ': ', .)" />
<br />
</xsl:if>
</xsl:for-each>
</xsl:for-each>
I haven't tested this but I think it will work.
Lars
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com]On Behalf Of
florian
Sent: Thursday, April 03, 2003 5:08 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] getting the longest text node
right now i do it via:
<xsl:for-each select="/*/*">
<xsl:for-each select="/*/*[local-name = local-name(current())]">
...
wrong?
hi!
how could i access the nodes though, if all i know is their
structure
but 'foo', 'orange' and 'apple' could be also any other name.. and
foo could also contain a flexible amount of children..
all i could think off is some kinda akward looking looping.. : /
ciao!
florian
I just thought I'd provide a variation. I can't make any
performance
claims vs. a sort in a for-each. Maybe someone else knows.
<xsl:template match="/">
<xsl:apply-templates select="//orange">
<xsl:sort data-type="number" order="descending"
select="string-length(.)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="orange">
<xsl:if test="position()=1">
<LongString type="{name()}" string-length="{string-length(.)}">
<xsl:value-of select="."/>
</LongString>
</xsl:if>
</xsl:template>
-rick
At 10:35 PM 3/11/03 +0100, you wrote:
hi!
i have a xml document like this:
<root>
<foo>
<apple>a</apple>
<orange>oiasd</orange>
</foo>
<foo>
<apple>hellooooo</apple>
<orange>test</orange>
</foo>
</root>
what would be the most performant and best way to get
the longest text of a apple node, and the longest text
of a orange node?
thanks alot!
ciao!
florian
XSL-List info and archive:
http://www.mulberrytech.com/xsl/xsl-list
Rick Taylor
XML Developer
PPDM Association
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list