# XML syntax highlighting

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?

-
minted might have better support? – Seamus Jul 25 '11 at 14:46
Care to reconsider the accepted answer? – krlmlr Oct 23 '13 at 19:32

XML language has very limited support for keywords. You can define more keywords yourself:

\lstset{
language=XML,
morekeywords={encoding,
xs:schema,xs:element,xs:complexType,xs:sequence,xs:attribute}
}

-

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,
}

\lstdefinelanguage{XML}
{
morestring=[b]",
morestring=[s]{>}{<},
morecomment=[s]{<?}{?>},
stringstyle=\color{black},
identifierstyle=\color{darkblue},
keywordstyle=\color{cyan},
}


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

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.

-
I want the comments in the XML which are inside <!-- --> in gray same as <? ?>. Can you tell me how to do it? – JHS Jun 9 '13 at 13:42

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]{!--}{--},
moredelim=[s][\color{black}]{>}{<},
moredelim=[s][\color{red}]{\ }{=},
stringstyle=\color{blue},
identifierstyle=\color{maroon}
}


Example output:

-
Finally, a style that really produces good XML formatting (for the documents I tested at least). I modified it a bit to get (almost) the same formatting as Firefox when it displays an XML file. \lstdefinelanguage{XML}{ columns=fullflexible, basicstyle=\rmfamily, commentstyle=\ttfamily\itshape\color{green!50!black}, morestring=[s]{"}{"}, morecomment=[s]{?}{?}, morecomment=[s]{!--}{--}, morecomment=[s]{!DOCTYPE}{]}, moredelim=[s][\color{black}]{>}{<}, moredelim=[s][\bfseries\color{black}]{\ }{=}, stringstyle=\color{blue}, identifierstyle=\bfseries\color{violet} }  – alfC Jan 4 at 4:36

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]{<!--}{-->},
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.

-
You forget the comments: morecomment=[s]{<!--}{-->},. – Paul Gaborit Oct 23 '13 at 16:01
@PaulGaborit: Thanks, edited. – krlmlr Oct 23 '13 at 19:28
one improvement: adding alsoletter=-: will allow the semicolon in attribute names (such as xmlns:xs) to be coloured and similarly for attribute names with a dash, e.g. list-type.  – greyshade Sep 2 '14 at 9:34

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},
`