• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

Extract the last word in an element tag XSLT

Greetings!

Is it possible to extract the last word in an element tag?

For example:

<TEXT> This is making me rip my hair out. </TEXT>

Is there a way to just get the last word, "out" out?  

I tried using the following, without success.

<xsl:param name="last" select="($arg/*[position() = last()])"/>

Any advice or guidance is greatly appreciated.

Best regards,
Jamie
0
jmc430
Asked:
jmc430
  • 4
  • 3
1 Solution
 
Geert BormansInformation ArchitectCommented:
Jamie,

"last" would only work for elements,
not for words seperated by spaces

you need to process the string recursively,
I will post an example in a minute


Cheers!
0
 
jmc430Author Commented:
Thanks Geert!!
0
 
Geert BormansInformation ArchitectCommented:
jmc430,

here it is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:call-template name="getLastWord">
            <xsl:with-param name="strParam" select="normalize-space(//TEXT)"/>
        </xsl:call-template>
        </xsl:template>
    <xsl:template name="getLastWord">
        <xsl:param name="strParam"/>
        <xsl:choose>
            <xsl:when test="contains($strParam, ' ')">
                <xsl:call-template name="getLastWord">
                    <xsl:with-param  name="strParam" select="substring-after($strParam, ' ')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="$strParam"/></xsl:otherwise>
        </xsl:choose>
       
    </xsl:template>
</xsl:stylesheet>
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Geert BormansInformation ArchitectCommented:
Jamie,

in order for the recursive template to work,
I add a normalize-space in the first call to the template
the reason is that otherwise there is a trailing space (possibly)
that breaks the test

in the otherwise, you can call your remove trailing dot template to remove the '.' after the out

cheers

Geert
0
 
jmc430Author Commented:
Thanks so much Geert!  You're totally awesome
0
 
jmc430Author Commented:
:)!
0
 
Geert BormansInformation ArchitectCommented:
you are welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now