Subject: finding the string before a special character
Posted By: srini Post Date: 12/10/2003 11:40:27 PM
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
Reply By: armmarti Reply Date: 12/11/2003 5:20:58 AM
Here is the stylesheet:


<?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
Reply By: srini Reply Date: 12/16/2003 6:57:38 AM
Hello Armmarti,
Thanks.
it is working fine.

srini

Go to topic 7642

Return to index page 985
Return to index page 984
Return to index page 983
Return to index page 982
Return to index page 981
Return to index page 980
Return to index page 979
Return to index page 978
Return to index page 977
Return to index page 976