Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

finding the string before a special character

Posted on 2003-12-10
2
Medium Priority
?
326 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 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

772 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