# Problems with XML Syntax Highlighting

I want to highlight syntax in XML files. I found a nice definition from krlmlr which I changed to my needs.

It works fine for Android's xml files:

But the result of a xml file with values in between the tags doesn't look great:

I don't mind to use two different language definitions but they should look similar to each other. I also tried other examples which I found but all of them color the angle brackets different from the tags. Using minted isn't an option because I use wissdoc (German page) as document class and it conflicts somehow with it.

Can someone give me a nice definition for second (non Android) XML please? Like I said, I don't care to use two different language definitions to make it easier :-)

Below is a minimal working example:

\documentclass{book}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{listings}

\definecolor{Maroon}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}

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

\begin{document}
\begin{lstlisting}[%
language=XML_android,
numbers=left,
frame=single,
breaklines=true,
caption={AndroidManifest.xml},
label={list:prototyp-AndroidManifest.xml}]
<!-- For own voice trigger. -->

<meta-data
<!-- Reference to trigger xml -->
android:resource="@xml/voice_trigger" />

\end{lstlisting}

\begin{lstlisting}[%
language=XML_android,
numbers=left,
breaklines=true,
frame=single,
caption={xml file},
label={list:arel-index.xml}]
<object id="model1">
<title><![CDATA[box]]></title>
<assets3d>
<model><![CDATA[html/resources/f50e387c699cd8c6afae2ae7c7aa3e81/box.zip]]></model>
<transform>
<translation>
<x>0.0</x>
<y>9.302986145</y>
<z>0.0</z>
</translation>
\end{lstlisting}

\end{document}

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – greyshade Sep 2 '14 at 9:07
• also, you might want to have a look at my answer here, but I fear it will run into the same problem.. – greyshade Sep 2 '14 at 9:26
• Please paste code as text (using the {} button) preferably as a complete test document. Your xml fragments are images which makes it hard to test any answer. – David Carlisle Sep 2 '14 at 9:44
• @greyshade I updated my question to include a MWE. You still need to get wissdoc.cls though. Link to the archive is provided in the question. – Thurion Sep 2 '14 at 11:01
• @Thurion instead of having the custom wissdoc I guess for your problem a simple \documentclass{book} \usepackage{xcolor} \usepackage{listings}  would do. – greyshade Sep 2 '14 at 11:32

I couldn't come up with a perfect solution right now, but juggling with the solution from what you linked and from my answer here I came up with the following, which isn't too bad:

\documentclass{book}

\usepackage{xcolor}
\usepackage{listings}

\definecolor{Maroon}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}

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

\lstdefinelanguage{XML_SYNTAX}{%
morekeywords={id},
alsoletter=-,
morestring=[b]",
stringstyle=\color[rgb]{0,0,1},
morecomment=[s]{<?}{?>},
morecomment=[s]{<!--}{-->},
morecomment=[s]{<!}{>},
moredelim=[s][\color{black}]{![}{]]},
moredelim=*[s][\color{Maroon}]{<}{>},
keywordstyle=\color{red}
}

\lstset{
% Basic design
backgroundcolor=\color[rgb]{0.9,0.9,0.9},
basicstyle={\small\ttfamily},
breaklines=true,
frame=l,
tabsize=2,
% Line numbers
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
% HTML formatting
language=XML_SYNTAX,
}

\begin{document}
\begin{lstlisting}[%
language=XML_android,
caption={AndroidManifest.xml},
label={list:prototyp-AndroidManifest.xml}]
<!-- For own voice trigger. -->

<meta-data
<!-- Reference to trigger xml -->
android:resource="@xml/voice_trigger" />

\end{lstlisting}

\begin{lstlisting}[%
language=XML_SYNTAX,
caption={xml file},
label={list:arel-index.xml}]
<object id="model1">
<title><![CDATA[box]]></title>
<assets3d>
<model><![CDATA[html/resources/f50e387c699cd8c6afae2ae7c7aa3e81/box.zip]]></model>
<transform>
<translation>
<x>0.0</x>
<y>9.302986145</y>
<z>0.0</z>
</translation>
\end{lstlisting}

\end{document}


You can set the style of the [CDATA[...]] parts as you like - since they start with <! some care must be taken when defining comment styles..

• @Thurion it has the downside of having to specify the keywords manually, i.e. not being completely syntax based anymore.. but I couldn't figure out how to get the keywords styles and have values between tags not take the same colour as the tags etc. – greyshade Sep 2 '14 at 12:32
• That's no big deal. I just have to look carefully for new keywords in new XML files. – Thurion Sep 2 '14 at 12:38