Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old December 11th, 2003, 12:40 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default finding the string before a special character

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.

srini
__________________
srini
 
Old December 11th, 2003, 06:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Here is the stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="allowed" select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_/'"/>

    <xsl:template match="emp">
        <xsl:for-each select="name">
            <xsl:call-template name="cut-bad-string">
                <xsl:with-param name="str" select="."/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="cut-bad-string">
        <xsl:param name="str"/>
        <xsl:param name="idx" select="1"/>

        <xsl:choose>
            <xsl:when test="string-length($str) &lt; $idx">
                <xsl:value-of select="$str"/>
                <br/>
            </xsl:when>
            <xsl:when test="string-length(normalize-space(translate(substring($str, $idx, 1), $allowed, ''))) = 0">
                <xsl:call-template name="cut-bad-string">
                    <xsl:with-param name="str" select="$str"/>
                    <xsl:with-param name="idx" select="$idx + 1"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring($str, 1, $idx - 1)"/>
                <br/>
            </xsl:otherwise> 
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
Regards,
Armen
 
Old December 16th, 2003, 07:57 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Armmarti,
Thanks.
it is working fine.

srini





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation for special character nazneen Java GUI 5 February 13th, 2006 03:13 PM
Special Character mg1966 Classic ASP Databases 2 December 13th, 2004 11:12 AM
XML - Special Character Formatting sean1230 XML 3 February 18th, 2004 05:17 AM
HELP!!! the problem of handling special character kemin Classic ASP Databases 1 December 13th, 2003 01:31 PM
Finding a specific character within a string tp194 Classic ASP Databases 2 October 12th, 2003 10:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.