Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL substring

Status
Not open for further replies.

Muddmuse

Programmer
Jul 31, 2001
85
0
0
US
I have a list of names in XML format.

<Name>John Doe</Name>
<Name>Jane Q. Doe</Name>

I need to display only the last name. I began using substring-after(Name,' ') but if the name has a middle initial I get that along with the last name. Is there a way to get a substring from the last occurrance of the space in XSL?
 
<xsl:template name=&quot;strAfterLast&quot;>
<!-- declare that it takes two parameters - the string and the char -->
<xsl:param name=&quot;string&quot; />
<xsl:param name=&quot;char&quot; />
<xsl:choose>
<!-- if the string contains the character... -->
<xsl:when test=&quot;contains($string, $char)&quot;>
<!-- call the template recursively... -->
<xsl:call-template name=&quot;strAfterLast&quot;>
<!-- with the string being the string after the character
-->
<xsl:with-param name=&quot;string&quot;
select=&quot;substring-after($string, $char)&quot; />
<!-- and the character being the same as before -->
<xsl:with-param name=&quot;char&quot; select=&quot;$char&quot; />
</xsl:call-template>
</xsl:when>
<!-- otherwise, return the value of the string -->
<xsl:eek:therwise><xsl:value-of select=&quot;$string&quot; /></xsl:eek:therwise>
</xsl:choose>
</xsl:template>



then to get the last index of a space you could stick the following inside a variable declaration
<xsl:call-template name=&quot;strAfterLast&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;Name&quot; />
<xsl:with-param name=&quot;char&quot; select=&quot; &quot; />
</xsl:call-template>



mike griffith

----------------------------
systems and control engineering
case western reserve university
mdg12@cwru.edu
 
If you start parsing this string based on spaces, you're kinda in trouble if a NAME is something like &quot;Martin Saint Louis&quot; or &quot;Robert J Mc Intyre&quot;.

You could try using JavaScript to split it into an array, and then evaluate each child of the array, but there are alot of variables to check for - &quot;Does the middle initial exist? If it does, does it have a period? If not, how do I know it's not part of the last name?&quot;

This is a good topic...

 
Great, thanks mgriffith.

I did have one problem implementing your suggestion. The transformation was failing here:

<xsl:with-param name=&quot;char&quot; select=&quot; &quot; />

It seems to be expecting an expression in the select attribute rather than a space. I simply did not pass this param but hardcoded the space in the template strAfterLast. Am I correct in saying select must equal an expression? If it worked for you could it be a parser version issue? I'm running msxml3.

JoeMcGarvey brings up a good point. I can use the xsl approach for my app but it could get sticky for certain applications.

 
yeah that doesn't look right.... try
<xsl:with-param name=&quot;char&quot; select=&quot;' '&quot; />
or
<xsl:with-param name=&quot;char&quot;> </xsl:with-param> mike griffith

----------------------------
systems and control engineering
case western reserve university
mdg12@cwru.edu
 
No luck with either of those. I'll keep at it. Thanks
 
I know you want to do this in XSL, but, what the hay!

<script language=&quot;JavaScript1.2&quot;>
function parseLastName() {
var theString = &quot;<xsl:value-of select='LastName'/>&quot;;
<![CDATA[
theSplitValues = theString.split(&quot; &quot;);
theLength = theSplitValues.length;
theLastName = theSplitValues[theLength-1];
theField.value = theLastName;
]]>
}
onload = parseLastName;
</script>


<input type=&quot;text&quot; id=&quot;theField&quot; name=&quot;LastName&quot;/>
Joe McGarvey - Web Application Developer
Paragraph, Inc. - Paragraph Publisher -
 
you can actually use javascript within an xsl

here's a shortened example of something that i did in the past...

<xsl:stylesheet
xmlns:xsl=&quot; version=&quot;1.1&quot;
xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot;
xmlns:user=&quot;>

<xsl:template match=&quot;item&quot;>
<xsl:value-of select=&quot;user:format_item(string(@help))&quot;/>
</xsl:template>

<msxsl:script language=&quot;JScript&quot; implements-prefix=&quot;user&quot;>
function format_item(item) {
return StrReplace(item, &quot;'&quot;, &quot;\\'&quot;);
}
function StrReplace(str1, str2, str3)
{
str1 = str1.split(str2).join(str3);
return str1;
}
</msxsl:script>

</xsl:stylesheet>

you're basically free to use whatever you want in javascript.... mike griffith

----------------------------
systems and control engineering
case western reserve university
mdg12@cwru.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top