Solved

finding the string before a special character

Posted on 2003-12-10
2
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

751 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