4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prosseek
  • 182,215
  • 215
  • 566
  • 871

6 Answers6

5

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
  • 322,767
  • 57
  • 360
  • 340
2

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

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
  • 18,795
  • 6
  • 56
  • 82
0

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
  • 2,127
  • 15
  • 16
0

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!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JiminyCricket
  • 7,050
  • 7
  • 42
  • 59
0
  • Select the region
  • Do M-% < RET &lt; RET !
Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40