Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't highlight xml syntax with highlight.js

I'm trying to print xml on an html page.

Example:

<-- language: lang-xml -->

<pre><code class="xml">
    <?xml version="1.0"?>
    <response value="ok" xml:lang="en">
        <text>Ok</text>
        <comment html_allowed="true"/>
        <ns1:description><![CDATA[
        CDATA does <not> escape <tags like="this"></tags></not>.
        ]]></ns1:description>
        <a></a> <a/>
    </response>
</code></pre>

I'd like to use highlight.js to highlight code on a page but there's a conflict with xml and html.

I only had success with some php code using <pre><code class="php"></code></pre>, haven't tried with other languages. The only way I found to display xml code is in a textarea, but I'd prefer to show the xml with some sweet syntax-highlighting.

I have made a jsfiddle to illustrate the problem.

Hope you can help.

like image 913
GabLeRoux Avatar asked Jun 09 '12 19:06

GabLeRoux


2 Answers

You should escape angle brackets with &lt; and &gt; when you want to display them literally. Otherwise you have actual markup within your <code> element that browsers try to interpret. This has nothing to do with any JavaScript applied to it.

like image 102
isagalaev Avatar answered Oct 16 '22 07:10

isagalaev


For what it's worth, Google Prettify does render XML blocks properly (without having to encode them as &lt;.

like image 41
aorth Avatar answered Oct 16 '22 08:10

aorth