[Tex/LaTex] Problems with XML Syntax Highlighting

listingsxml

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:
Android's xml files

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

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]{<!--}{-->},
  commentstyle=\color{darkgreen},
  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. -->
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />

        <meta-data
            <!-- Reference to trigger xml -->
            android:name="com.google.android.glass.VoiceTrigger"
            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}

Best Answer

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:

enter image description here

\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]{<!}{>},
    commentstyle=\color{darkgreen},
    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]{<!}{>},
    commentstyle=\color{darkgreen},
    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. -->
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />

        <meta-data
            <!-- Reference to trigger xml -->
            android:name="com.google.android.glass.VoiceTrigger"
            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..