Solved

finding the string before a special character

Posted on 2003-12-10
2
318 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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 video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

16 Experts available now in Live!

Get 1:1 Help Now