Solved

finding the string before a special character

Posted on 2003-12-10
2
319 Views
Last Modified: 2012-05-05
Suppose i have an xml file.
i have to write an xsl file so that it should allow characters from
a-z and A-Z and numbers and '_' and '/'.
if the string is having any other character then it should display the string before the special character.
if the string is not having any other special characters then it should display normally.

my xml file is:

<?xml version="1.0"?>
<emp>
 <name>srini$sdg</name>
 <name>rama</name>
 <name>rameshs@dg</name>
 <name>r)amki</name>
 <name>ragi</name>
</emp>

in this case the output should be :
srini
rama
rameshs
r
ragi
pease give a solution to this.
thanks
srini.
0
Comment
Question by:srini_r
2 Comments
 
LVL 6

Expert Comment

by:metalmickey
ID: 9919513
you could use the substring-before function

0
 
LVL 6

Accepted Solution

by:
PeterCiuffetti earned 25 total points
ID: 9920182
Here's a stylesheet that does what you want.  It uses the translate function to create a variable containing only the special charaters.  it then uses the first of these in the subbstring-before to output the beginning

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="name">
      <!-- translate out the non-special characters, leaving the special characters -->
      <xsl:variable name="specialChars" select="translate(.,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_/','')"/>
      <xsl:choose>
            <!-- If there are no special characters, output the whole name -->
            <xsl:when test="string-length($specialChars) = 0">
                  <xsl:value-of select="."/>
            </xsl:when>
            <!-- Otherwise output the string before the first special character -->
            <xsl:otherwise>
                  <xsl:variable name="firstSpecialChar" select="substring($specialChars,1,1)"/>
                  <xsl:value-of select="substring-before(.,$firstSpecialChar)"/>
            </xsl:otherwise>
      </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Pete
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now