Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 27th, 2006, 06:07 AM
Authorized User
Points: 108, Level: 2
Points: 108, Level: 2 Points: 108, Level: 2 Points: 108, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , United Kingdom.
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
:)


Reply With Quote
  #2 (permalink)  
Old April 27th, 2006, 04:38 PM
Authorized User
 
Join Date: Apr 2006
Location: , , United Kingdom.
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>


Reply With Quote
  #3 (permalink)  
Old April 27th, 2006, 04:42 PM
Authorized User
 
Join Date: Apr 2006
Location: , , United Kingdom.
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. :)

Reply With Quote
  #4 (permalink)  
Old April 28th, 2006, 08:22 AM
Authorized User
Points: 108, Level: 2
Points: 108, Level: 2 Points: 108, Level: 2 Points: 108, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , United Kingdom.
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.

Reply With Quote
  #5 (permalink)  
Old May 5th, 2006, 04:54 AM
Authorized User
Points: 108, Level: 2
Points: 108, Level: 2 Points: 108, Level: 2 Points: 108, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , United Kingdom.
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>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:09 PM.


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