 |
| 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
|
|
|
|

August 1st, 2011, 12:54 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Help with XSL String manipulation
Hi,
I have two tags containing two strings for example
<ConsName>Sarah Hunter</ConsName>
and
<SampleName>Hunter</SampleName>
Based on the above two strings, I have to get the output as "Sarah" by string manipulation in XSL.
Request the members to help me in this regard
Regards,
Ramakanth
|
|

August 1st, 2011, 01:13 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Please tell us whether you use XSLT 2.0 or 1.0.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 1st, 2011, 01:44 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Martin, I am using version 1.0
Thanks
Ramakanth
|
|

August 1st, 2011, 01:51 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Well XSLT/XPath 1.0 are pretty limited when it comes to string manipulation http://www.w3.org/TR/xpath/#section-String-Functions, you haven't really explained how complicated the input can get but perhpas
Code:
<xsl:template match="ConsName">
<xsl:value-of select="normalize-space(substring-before(., //SampleName))"/>
</xsl:template>
gives you an idea as to what is possible.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 2nd, 2011, 03:22 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
I have tried the code that you provided. However it shows me empty string instead of the expected
As for the inputs, it is always, a combination of surname and firstname. The second string is always the surname. So I wanted to pull out the firstname using the two strings
Can you please provide any other alternative
Regards,
Ramakanth
|
|

August 2nd, 2011, 03:31 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Good news..I could work out a solution and below is the code
<xsl:value-of select="(substring-before(//ConsName,' '))"/>
Thanks very much for your help
Regards,
Ramakanth
|
|

August 2nd, 2011, 08:59 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Martin,
I again faced the problem when the input is more than 2 words.
For example,
If <ConsName>Sarah Lake Hunter </ConsName>, out of which Sarah Lake is the First name, then my solution will only show Sarah
Typically, the solution would be to show the string other than the present in the surname
<Surname>Hunter</Surname>
Therefore I have to compare"Sarah Lake Hunter" and "Hunter" and produce "Sarah Lake"
Please suggest any solution
Regards,
Ramakanth
|
|

August 2nd, 2011, 09:05 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
I don't see why my first suggestion does not solve the problem as described so far.
When I use the sample input
Code:
<Root>
<ConsName>Sarah Lake Hunter </ConsName>
<Surname>Hunter</Surname>
</Root>
and the stylesheet
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="Root/ConsName"/>
</xsl:template>
<xsl:template match="ConsName">
<xsl:value-of select="normalize-space(substring-before(., //Surname))"/>
</xsl:template>
</xsl:stylesheet>
the output is "Sarah Lake".
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 2nd, 2011, 10:05 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Martin, May be I would not have tested properly
Anyhow the below code
<xsl:value-of select="(substring-before(//ConsName,//SurName))" /> works fine
Many thanks for your help in giving me a head start
Regards,
Ramakanth
|
|
 |