Results 1 to 6 of 6

Thread: Parsing problem with characters " & < > "

  1. #1
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parsing problem with characters " & < > "

    I'm trying to parse a line from a text file (.svm file) but when the text
    contains the characters & or < or > the text doesn't get parsed.
    It works fine if the text doesn't contain those 3 characters however.
    Whats the problem? Why won't it recognize those 3 characters?

    The line to parse is
    Notes=" any characters "

    I made the function match everything between the quotes
    and if nothing is between the quotes is will return **No Notes**

    Code:
    		function findnotes( notes )
    		{ 
      		    var notes // if no match, use this 
      		    var notesArray = notes.match(/".+"/gi); 
      		 	    if (notesArray)
    			 	{ 
      		 	 	notes = ""; 
      		 	 	    for (var i = 0; i < notesArray.length; i++)
    				    { 
      		 		    notes += notesArray[i]; 
          	 		    } 
      			 	} 
    				else
    				{
    				notes = "**No Notes**";
    				}
    				return notes;
    		} 	
    		
    		function convert_SVM_to_XML( p_inp, p_t )
    		{
    			sXML = "";
    			sXML += "<svm>";
    			fso = new ActiveXObject( "Scripting.FileSystemObject" );
    			try
    			{
    				tfRead = fso.OpenTextFile( p_inp.value, 1/*for reading*/, false );
    			}
    			catch(e)
    			{
    				alert("Hey Dumb Dumb! You need to browse for a setup!");
    				return;
    			}
    			while ( !tfRead.AtEndOfStream )
    			{
    				s = tfRead.ReadLine();
    				if( s == "[GENERAL]" )
    				{
    					s = tfRead.ReadLine();
    					sXML += "<par name='Notes'>" + findnotes( s ) + "</par>";
    					s = tfRead.ReadLine();
    The output is xsl stylesheet:
    Code:
    		function showPar( pmode, pdescname, pparname, punit, ponlyDiff )
    		{
    			ps = "";
    			if(( pmode == 1 ) || ( pmode == 2 ))
    				ps+="									"+pdescname+" <a style='font-size: 12px; font-weight:bold; color:"+arrColors[pmode]+";'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
    			else
    			{
    				if( ponlyDiff == 1 )
    				{
    					ps+="									<xsl:if test='svms/svm[1]/par[@name=\""+pparname+"\"] != svms/svm[2]/par[@name=\""+pparname+"\"]'>";
    					ps+="										"+pdescname+" <a style='font-size: 12px; font-weight:bold; color:"+arrColors[1]+";'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> <b>/</b> <a style='font-size: 12px; font-weight:bold; color:"+arrColors[2]+";'><xsl:value-of select='svms/svm[2]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
    					ps+="									</xsl:if>";
    				}
    				else
    				{
    					ps+="									<xsl:choose>";
    					ps+="										<xsl:when test='svms/svm[1]/par[@name=\""+pparname+"\"] != svms/svm[2]/par[@name=\""+pparname+"\"]'>";
    					ps+="											"+pdescname+" <a style='font-size: 12px; font-weight:bold; color:"+arrColors[1]+";'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> <b>/</b> <a style='font-size: 12px; font-weight:bold; color:"+arrColors[2]+";'><xsl:value-of select='svms/svm[2]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
    					ps+="										</xsl:when>";
    					ps+="										<xsl:otherwise>";
    					ps+="											"+pdescname+" <a style='font-size: 12px; font-weight:normal;'><xsl:value-of select='svms/svm[1]/par[@name=\""+pparname+"\"]'/></a> <b>/</b> <a style='font-size: 12px; font-weight:normal;'><xsl:value-of select='svms/svm[2]/par[@name=\""+pparname+"\"]'/></a> "+punit+"<br/>";
    					ps+="										</xsl:otherwise>";
    					ps+="									</xsl:choose>";
    				}
    			}
    			return ps;
    		}
    		
    		
    		function showFile( mode, onlyDiff )
    		{
    			oxml = new ActiveXObject('Microsoft.XMLDOM');
    			oxml.async = false;
    			strXML1 = "";
    			strXML2 = "";
    			if(( mode == 1 ) || ( mode == 3 ))
    			{
    				strXML1 = convert_SVM_to_XML( document.all.inpFile1 );
    			}
    			if(( mode == 2 ) || ( mode == 3 ))
    			{
    				strXML2 = convert_SVM_to_XML( document.all.inpFile2 );
    			}
    			oxml.loadXML( "<?xml version='1.0'?><svms>" + strXML1 + strXML2 + "</svms>" );
    			//alert(oxml.xml);
    			var xsldom=new ActiveXObject("Microsoft.XMLDOM");
    			xsldom.async=false;
    			strxsl="";
    			strxsl+="<?xml version='1.0'?>";
    			strxsl+="<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='3.0'>";
    			strxsl+="<xsl:template match='/'>";
    			strxsl+="	<table border='1' width='100%' style='background-color: black' height='100%' cellpadding='1' cellspacing='1' align='center'>";
    			strxsl+="		<tr>";
    			strxsl+="			<td align='right'>";
    			strxsl+="				<table width='240' height='100' style='background-color: gray' cellpadding='0' cellspacing='0' border='0'>";
    			strxsl+="					<tr height='20'><td>";
    			strxsl+="						<table cellpadding='2' cellspacing='1' width='100%' border='0' height='100%'>";
    			strxsl+="		    				<tr>";
    			strxsl+="		    					<td align='center' style='font-family: arial; font-size: 13px; font-weight: bold; color: #FFFFFF; background-color: gray;'>";
    			strxsl+="		    						SETUP NOTES";
    			strxsl+="		    					</td>";
    			strxsl+="		    				</tr>";
    			strxsl+="						</table>";
    			strxsl+="					</td></tr>";
    			strxsl+="					<tr><td>";
    			strxsl+="						<table cellpadding='2' cellspacing='1' width='100%' height='100%' border='0'>";
    			strxsl+="							<tr>";
    			strxsl+="								<td valign='top' style='font-family: Arial; font-size: 11px; font-weight: normal; color: black; background-color: white;'>";
    			strxsl+=									showPar( mode, "", "Notes", "", onlyDiff );
    Last edited by SuperLes2006; 01-16-2006 at 07:16 PM. Reason: added code tag

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You need to XML-encode them.
    Code:
    notes = notes.replace(/&/, "&amp;").replace(/</, "&lt;").replace(/>/, "&gt;");
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Brilliant!!! That worked. Thanks
    I also added /gi so it would recognize those special characters more than once.

    Code:
    		function findnotes( notes )
    		{
      		    var notes // if no match, use this
      		    var notesArray = notes.match(/".+"/gi);
      		 	    if (notesArray)
    			 	{
      		 	 	notes = "";
      		 	 	    for (var i = 0; i < notesArray.length; i++)
    				    {
      		 		    notes += notesArray[i];
          	 		    }
      			 	}
    				else
    				{
    				notes = "**No Notes**";
    				}
    				return notes.replace(/&/gi, "&amp;").replace(/</gi, "&lt;").replace(/>/gi, "&gt;"); notes;
    Now...My only problem is that the output doesn't show more than one space at a time. For example:

    Input : Notes="Just_____A__Test!"
    I had to use _ for a space because this board eliminates the extra spaces too.

    The output would be: "Just A Test!"

    ARGH!!
    Last edited by SuperLes2006; 01-16-2006 at 07:29 PM.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You only need g, not i; i is for case insensitivity, which doesn't apply here. The problem with your spaces is a rendering one. HTML automatically, on encountering more than one space, renders it as just one space, to avoid whitespace being shown on the site. Try:
    Code:
    notes = notes.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/ /g, "&nbsp;");
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey! Your right, I didn't need the i for case insensitivity

    This code don't work though
    Code:
    .replace(/ /g, "&nbsp;");
    but this Works like a charm!!!!
    Code:
    .replace(/ /g, "&#160");
    Thanks for all the help

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Really? Hm, odd. Ah well.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •