views:

124

answers:

7

In the HTML file, I need to show some XML code. The problem is that I can't use

<pre>..</pre> 

to show '<' and '>'.

What would be the solution for this problem?

ADDED

From the answer, replacing '<' and '>' to &lt; and&gt; can be a solution. I'm an Emacs user, are there Emacs tools/magic to do that automatically? I mean, I can use search and replace, but I expect Emacs can do it by 'select region' -> 'M-x replace_xml' or something.

+2  A: 

You need to replace < by &lt; and > by &gt;. How to do this depends on the server side language in question.


Update: as per your update: this is not programming related anymore. I think http://superuser.com is a better place to ask software related questions.

BalusC
A: 

If you can use replacement routines (in a server side language or with javascript) then you should replace all ampersangs (& - with &amp;) and less than signs (< with &lt;) and optionally greater than signs (> with &gt;).

Alternatively you can wrap the xml in escaped markup (start: <![CDATA[ end: ]]>) - but this isn't much different from using <pre>...</pre>

Rudu
BalusC
Rudu
And while it's accurate that *pure html* doesn't accept `<![CDATA[` tags, XHTML **does** - depending on how/what's being rendered it may well be an applicable solution.
Rudu
+1  A: 

As already mentioned, you need to escape the XML. For robustness I would also escape single and double quotes too. Note that CDATA and <pre> can cause you problems if, for any reason, your XML document includes ]]> or </pre> in it.

You can get away with doing a straight string substitution for the escaping, but if you do, make sure you escape & to &amp; before doing any of the other escapes.

dty
+2  A: 

You need to escape < as &lt; and & as &amp;. Optionally, for consistency, you can escape > as &gt;

To do this automatically in Emacs, if you're in HTML mode, you can select the code that you would like to escape, and run M-x sgml-quote.

Brian Campbell
M-x sgml-quote is exactly what I need. Thanks!
prosseek
A: 

As other have noted, you need to escape the xml markup to display it in html.

Take a look at xmlverbatim stylesheet: It does that as well as pretty printing and colorizing.

If you google around there are several stylesheets to do similar formatting.

Steven D. Majewski
A: 

Do a substitution using a programming language without Emacs.

For Python:

#Make a copy just in case.
#Open file.
#Read lines.
for line in lines:
    line = line.replace("<pre>", "&lt;").replace("</pre>", "&gt;")
#Output to file.
#Enjoy!
JiminyCricket
A: 
  • Select the region
  • Do M-% < RET &lt; RET !
Jérôme Radix