Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I use the following code to include some XML code:

\lstset{language=XML}
\begin{lstlisting}
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
   xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="points">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="point">
          <xs:complexType>
            <xs:attribute name="x" type="xs:unsignedShort" use="required" />
            <xs:attribute name="y" type="xs:unsignedShort" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
\end{lstlisting}

But only ?xml version is highlighted. Is there any other predefined XML syntax highlighting?

share|improve this question
    
minted might have better support? –  Seamus Jul 25 '11 at 14:46
2  
Care to reconsider the accepted answer? –  krlmlr Oct 23 '13 at 19:32
add comment

5 Answers

up vote 15 down vote accepted

XML language is very limited supported. You can define more keywords yourself:

\lstset{
  language=XML,
  morekeywords={encoding,
    xs:schema,xs:element,xs:complexType,xs:sequence,xs:attribute}
}
share|improve this answer
add comment

You can define your own language. The best I could get so far:

\usepackage{listings}

\usepackage{color}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
  showstringspaces=false,
  commentstyle=\color{gray}\upshape
}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}% list your attributes here
}

Here is an example of highlighted XML with the settings above:

XML syntax highlighting

I'd like to set the syntax bits such as <, >, </, =, : in gray as well, because they don't hold relevant information, but this would brake other parts of the highlighting. Maybe someone else can improve it and send a fix to the maintainer of listings package.

share|improve this answer
    
I want the comments in the XML which are inside <!-- --> in gray same as <? ?>. Can you tell me how to do it? –  Jun Aid Jun 9 '13 at 13:42
add comment

I was looking for a solution that will apply color on <, </, > and />. I discovered that applying basicstyle in the lstdefinelanguage has solved my problem.

With the basicstyle, I defined the style that is applied for all the code content without regards of the content type (identifiers, keywords, ...) and then, the other style will be applied. (added basicstyle=\ttfamily\color{darkblue}\bfseries)

\lstdefinelanguage{XML}
{
  basicstyle=\ttfamily\color{darkblue}\bfseries,
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}% list your attributes here
}
share|improve this answer
1  
I cannot see how this colorizes the missing characters. At least it did not work for me. Could you please double check if you missed something and add a screenshot please? –  JJD Sep 18 '12 at 21:53
add comment

Here's another style which is rather hacky and stingy about additional whitespace because of the attribute delimiter that starts with a space:

\definecolor{maroon}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\lstdefinelanguage{XML}
{
  basicstyle=\ttfamily,
  morestring=[s]{"}{"},
  morecomment=[s]{?}{?},
  morecomment=[s]{!--}{--},
  commentstyle=\color{darkgreen},
  moredelim=[s][\color{black}]{>}{<},
  moredelim=[s][\color{red}]{\ }{=},
  stringstyle=\color{blue},
  identifierstyle=\color{maroon}
}

Example output:

Screenshot

share|improve this answer
add comment

A synthesis of the previous answers that works best for me, especially w.r.t. highlighting the angle brackets. Using SVG Color names (svgnames):

\lstdefinelanguage{XML}
{
  basicstyle=\ttfamily\footnotesize,
  morestring=[b]",
  moredelim=[s][\bfseries\color{Maroon}]{<}{\ },
  moredelim=[s][\bfseries\color{Maroon}]{</}{>},
  moredelim=[l][\bfseries\color{Maroon}]{/>},
  moredelim=[l][\bfseries\color{Maroon}]{>},
  morecomment=[s]{<?}{?>},
  morecomment=[s]{<!--}{-->},
  commentstyle=\color{DarkOliveGreen},
  stringstyle=\color{blue},
  identifierstyle=\color{red}
}

The order of the \more... commands seems to be important. Note that you need a font that supports bold-face to be Using \ttfamily with \bfseries (or how to enable bold in fixed-width font). Code is at GitHub.

Example output

share|improve this answer
    
You forget the comments: morecomment=[s]{<!--}{-->},. –  Paul Gaborit Oct 23 '13 at 16:01
    
@PaulGaborit: Thanks, edited. –  krlmlr Oct 23 '13 at 19:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.