views:

47

answers:

3

I'm trying to get a page to validate (http://validator.w3.org) and it complains about some xml I have inside a script tag.

How can I resolve this? Am I supposed to have something around the content of my script tag saying "don't look at me"?

Line 68, column 114: end tag for element "STR_PROCESSING" which is not open

>Processing....</STR_Processing>

Code:

<script type="text/javascript" defer="defer">
var sML_XML='<STR_Processing>Processing....</STR_Processing><STR_OK>...';
</script>
A: 

If you are using tag in strings you can do this :

 var yourHTML = "<STR_PROCESSING>YourData<" + "/STR_PROCESSING>";
HoLyVieR
This works, but is inefficient. Escaping as per rhino's answer and the Validator's own FAQ is the right solution here.
David Dorward
+2  A: 

You have to place a backslash (\) before the slashes, when it is in a script.
Look here: http://www.htmlhelp.com/tools/validator/problems.html#script

For example:
var test="<b>something<\/b>";
instead of:
var test="<b>something</b>";

This should validate with no errors.

rhino
+1. If you look directly under the error message output by the validator then you should see an "If this error occurs inside a script element" message that links to the Validator's own FAQ and then to that page.
David Dorward
@David Dorward, that's how I got that page. :) Long time ago I've seen it but forgot its address, but I remembered that I have navigated to it from the validator's page, so I've created a wrong code to get the error and the page's address. :P
rhino
A: 

The correct XML way is to use CDATA section identifiers:

<script type="text/javascript">//<![CDATA[
/* code */
//]]></script>
Lekensteyn
The error message is one that occurs when the document is HTML, not XHTML, so this wouldn't solve the problem.
David Dorward
Lekensteyn
Only the `<` or the `/` would need to be escaped (it doesn't matter which one, the point is that `</` shouldn't appear inside an element defined by the DTD as containing CDATA), and a simple `\/` as per rhino's answer is all that is needed. Using Unicode escape sequences would just be time consuming and make the script more difficult to read.
David Dorward
Ok you win :p Those are not Unicode escape sequences. Unicode escape sequences looks like `\uNNNN`.
Lekensteyn