Welcome to WebmasterWorld Guest from 54.145.246.183

Forum Moderators: httpwebwitch

XSLT Question

Need to select last node in a range with a value

   
4:20 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for example
<items>
<item name="F1">joe</item>
<item name="F2">smith</item>
<item name="F3">5</item>
<item name="F4">4</item>
<item name="F5">3</item>
<item name="F7" />
<item name="F9">foo</item>
</items>

I need to be able to select the last value between items "F3" and "F8" that has a value, in this case <item name="F5">3</item>.

How would I write that in an XSLT?

Thanks.

4:30 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it, I think

<xsl:apply-templates select="node()[substring-after(name(),'F') &lt; 9 and substring-after(name(),'F') &gt; 2 and .!= '']" />

<xsl:template match="node()[substring-after(name(),'F') &lt; 9 and substring-after(name(),'F') &gt; 2]">
<xsl:if test="position() = last()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>

4:30 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



should change that to a call-template probably and use a name
4:33 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you've got it.
position() and last() do the trick

nice one Gibble

5:17 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I presume the template was needed to create a "set", but is there a way to have written it without it?
11:43 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I suppose you could use the same XPATH in an <xsl:for-each>.

Have you tried combining the "node()[substring-after(name(),'F' ..." part with the position() and last() comparison? Maybe you could do the whole thing in one complex expression.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month