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 November 28th, 2003, 01:55 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to restrict strings with particular characters

Suppose i have an XML file, it contains some strings, now i have to write an xsl file to display the strings. there are some constraints to display the strings.
1. it should not display the strings with some special characters like +,/,=, etc.
2. it should display strings only with characters and _ .
3. if it having any other special characters it should display only ?.
please help me.

How can we validate it?
suppose this is the xml file, here it should display only srinir and ramesh_l
<?xml version="1.0" encoding="UTF-8"?>
<emp>
 <name>srini r</name>
 <name>ralla+ srinu</name>
 <name>(rama)</name>
 <name>srinir</name>
 <name>ramesh_l</name>
</emp>

please help me.
thanks in advance.
srini.

srini
__________________
srini
 
Old November 28th, 2003, 02:58 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

Try this (not tested):

Code:
    <xsl:variable name="allowable-characters" select="'#x20;#x9;#xD;#xA;abcdef...ABCDEF...123...'"/>
     <xsl:template match="name">
        <xsl:choose>
            <xsl:when test="contains(translate(., '+-/=', '++++'), '+')"/>
            <xsl:when test="string-length(translate(., $allowable-characters, '')) > 0">?#xA0;</xsl:when>
            <xsl:otherwise><xsl:value-of select="."/>#xA0;</xsl:otherwise> 
        </xsl:choose>   
     </xsl:template>
If you'll have problems with the code, let me know.

Regards,
Armen
 
Old November 28th, 2003, 03:00 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

Ampersands have disappeared (as always) in character references. Put them, don't forget :)
 
Old November 28th, 2003, 03:23 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That is not working. i am getting output as

<?xml version="1.0" encoding="UTF-16"?>
 ?#xA0;

 ?#xA0;
 ?#xA0;
 ?#xA0;
if i want to generate xml file from it.

srini
 
Old November 28th, 2003, 04:03 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

Srini,

you have to replace "..." in the "allowable-characters" variable value with proper sequences. Since Unicode has very big set of characters, it is more convenient to have a sequence of allowable characters (as I understand these allowable characters are all whitespace characters, all letters(small and capital) and digits). So, when I'm applying the source

<emp>
 <name>xyz</name>
 <name>ab+abc</name>
 <name>-fff-)</name>
 <name>abc</name>
 <name>def</name>
</emp>

to the stylesheet I've posted (without any change; though I've posted an excerpt which really needs some editing and I supposed you'll do the necessary post-editing) I'm getting
Code:
 ? 

 
 abc 
 def
I just wanted to show the idea behind.

Armen
 
Old November 28th, 2003, 04:36 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Armmarti,
i did'nt understand your solution properly. i think you did not understand the question properly. what ever the strings which are there in the xml file, our xslt should display only strings with allowed characters.
allowed charecters are
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789 and _(underscore).
if the string contains other than these chracters it should not display the string.
srini

srini
 
Old November 28th, 2003, 07:03 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

You said that:

Quote:
quote:
1. it should not display the strings with some special characters like +,/,=, etc.
2. it should display strings only with characters and _ .
3. if it having any other special characters it should display only ?.
From this I understand that you are separating all characters into 3 groups:
1. Those characters in the ASCII set which are niether letters nor digits nor underscore.
2. Those characters in the ASCII set which are letters or digits or underscore.
3. All other characters(potentially in Unicode)

So, my first solution is doing that separation. And I think that you didn't look at the code even before running it: you tried to run it without understanding it. We try to give hints or ideas to help people to accomplish their tasks and sometimes we give whole stylesheets when we think that it's really necessary, and any excerpt or comment is not satisfactory.
Nevertheless, in your last post you say that you need only such strings which don't contain anything except characters in the set you specified. In that case, you must do the following:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <head>
                <title></title>
            </head>
            <body>
                <table border="1">
                    <tr>
                        <th>Original strings</th>
                        <th>Filtered strings</th>
                    </tr>
                    <xsl:apply-templates select="emp/name"/>
                </table>
            </body>
        </html>
    </xsl:template>   
    <xsl:variable name="allowable-characters" select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'"/>
     <xsl:template match="name">
         <tr>
             <td>
                 <xsl:value-of select="."/>
             </td>
            <td>
                <xsl:choose>

                    <xsl:when test="string-length(translate(., $allowable-characters, '')) > 0">?</xsl:when>
                    <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise> 
                </xsl:choose>   
            </td>
         </tr>
     </xsl:template>       
</xsl:stylesheet>
Armen





Similar Threads
Thread Thread Starter Forum Replies Last Post
Best approach to replace characters with strings lumbrigack XSLT 8 June 16th, 2008 12:38 PM
Separating strings and replacing characters sunrain XSLT 4 April 7th, 2008 09:33 AM
Restrict elements guozhang XML 0 April 28th, 2004 02:26 PM





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