Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2006, 06:07 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default String formatting

Bit by bit I think I getting to grips with this. My next problem/issue is I'm trying to un-pack peoples names into Title, first name & last name. The name appears as SMITH/JOHNMR & JONES/ANNEMRS

<Name>SMITH/JOHNMR</Name>

I can extract the last name with:
<xsl:value-of select="substring-before(Name, '/')" />

I've tried getting the length of the name string and then checking the last 2 characters to see if they are "MR" (or "MS"etc.) or the last 3 to see if they are "MRS" or "SIR" etc......

What I've got is:

<xsl:variable name="len" select="string-length(Name)" />
<xsl:choose>
   <xsl:when test="substring(Name, $len-2) = 'MR'">
      <xsl:element name="Title">MR</xsl:element>
      <xsl:element name="FirstName">
         <xsl:value-of select="substring-after(Name, '/')" />
      </xsl:element>
      <xsl:element name="LastName">
         <xsl:value-of select="substring-before(Name, '/')" />
      </xsl:element>
   </xsl:when>
</xsl:choose>
Unfortunately this doesn't seem to work & it will still have the MR at the end of the firstname.

Any ideas anyone?

As always - thanks in advance
:)


 
Old April 27th, 2006, 04:38 PM
Authorized User
 
Join Date: Apr 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, just tried this on localhost:

input XML
------------
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="help1.xsl"?>
<root>
   <Name>SMITH/JOHNMR</Name>
   <Name>JONES/ANNEMRS</Name>
</root>




XSL
---
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>


<xsl:variable name="len" select="string-length(root/Name/text())" />
<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="contains(substring-after(root/Name, '/'), 'MR')">
            <xsl:element name="Title">MR</xsl:element>
            <xsl:element name="FirstName">
                 <xsl:value-of select="substring-after(substring-before(root/Name/text(), substring(root/Name/text(), ($len - 1), 2)), '/')" />
             </xsl:element>
             <xsl:element name="LastName">
                 <xsl:value-of select="substring-before(root/Name, '/')" />
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            blah blah
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


 
Old April 27th, 2006, 04:42 PM
Authorized User
 
Join Date: Apr 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just been thinking, perhaps try the MRS first, then MISS (changing where MR is & '($len-1),2' bit), otherwise its MR.

That is if you just have MR, MISS & MRS. Would be easier if you had, SMITH/JOHN_MR. Then you could just substring-after(., '_').

Hope this helps in some way. :)

 
Old April 28th, 2006, 08:22 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brilliant, works a treat. Many thanks.

There might be a few problems with titles such as LORD but they are the minority so I'll choose to live with these.

This is all travel related and that's how the name appears on the booking systems etc.

 
Old May 5th, 2006, 04:54 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just in case anyone is interested/cares, I've modified the code slightly to look for the last x characters instead of using "contains" (cuts doen the lines of code):

Code:
      <xsl:variable name="len" select="string-length(FirstName/Name/text())" />
       <xsl:choose>

         <xsl:when test="substring(FirstName/Name/text(), ($len - 1), 4) = 'MISS' or 
                         substring(FirstName/Name/text(), ($len - 1), 4) = 'LADY' or 
                         substring(FirstName/Name/text(), ($len - 1), 4) = 'PROF' or 
                         substring(FirstName/Name/text(), ($len - 1), 4) = 'MSTR' or 
                         substring(FirstName/Name/text(), ($len - 1), 4) = 'CAPT'">
           <xsl:element name="Title">
             <xsl:value-of select="substring(FirstName/Name/text(), ($len - 3), 4)"/>
           </xsl:element>
           <xsl:element name="FirstName">
             <xsl:value-of select="substring(FirstName/Name/text(), 1, ($len - 4))" />
           </xsl:element>
         </xsl:when>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting string with comma arnabghosh SQL Server 2005 1 October 16th, 2007 01:38 AM
String Formatting sumith ASP.NET 2.0 Basics 1 October 3rd, 2007 07:35 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.