xsl-list
[Top] [All Lists]

[xsl] creating tags around a string

2006-04-17 14:17:57
Hello,

I am trying to create some tags around a string of names and was wondering 
what is the best way to do this. I have come up with the below xslt, but 
it will only work limited instances. Any other ideas would be grand. For 
example, if I have any middle names or more than a first/last name, the 
xslt breaks.

Thanks!
-troy

=====================================================

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:foo="http://whatever";>
        <xsl:output method="text"/>
        <xsl:function name="foo:mesplit">
                <xsl:param name="inString"/>
                <xsl:param name="delimiter"/>
                <xsl:variable name="tokenizedSample" select="
tokenize($inString,$delimiter)"/>
                <xsl:for-each select="$tokenizedSample">
                        <xsl:variable name="thisstick">
                                <xsl:value-of select="."/>
                        </xsl:variable>
                        <xsl:choose>
                                <xsl:when test="contains(.,',')">
                                        <xsl:value-of select="
foo:mesplit($thisstick,',')"/>
                                </xsl:when>
                                <xsl:when test="contains(., ' ')">
                                        <xsl:value-of select="foo:mesplit( 
normalize-space($thisstick),' ')"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:if test=".">
                                                <xsl:if test="
index-of($tokenizedSample, .) = 1">
                                                        <xsl:text>  </
xsl:text>
                                                        <xsl:text>
&lt;fname&gt;</xsl:text>
                                                                <
xsl:value-of select="."/>
                                                        <xsl:text>
&lt;/fname&gt;</xsl:text>
                                                </xsl:if>
                                                <xsl:if test="
index-of($tokenizedSample, .) = 2">
                                                        <xsl:text>
&lt;lname&gt;</xsl:text>
                                                                <
xsl:value-of select="."/>
                                                        <xsl:text>
&lt;/lname&gt;</xsl:text>
                                                </xsl:if>
                                        </xsl:if>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:for-each>
        </xsl:function>
 
<xsl:template match="/">
                <xsl:value-of select="foo:mesplit('Al Stick, Tom She, Dick 
Burg and Harry Ward', 'and')"/>
        </xsl:template>
</xsl:stylesheet>

======================

desired output:

<fname>Al</fname><lname>Stick</lname>
<fname>Tom</fname><lname>She</lname>
<fname>Dick</fname><lname>Burg</lname>
<fname>Harry</fname><lname>Ward</lname>


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--