# 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

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

-

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? – Jun Aid Jun 9 at 13:42

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