[Tex/LaTex] XML syntax highlighting does not work

highlightinglistingsxml

How do I make comments highlighted in the form:

<!-- ======= Processing Root tag =======   -->

. This is not working.

\documentclass[11pt]{scrartcl}
\usepackage{listings, 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}

\lstloadlanguages{XML}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  morecomment=[s][\color{orange}]{<!--}{-->},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}
}

\lstdefinestyle{listXML}{language=XML, extendedchars=true,  belowcaptionskip=5pt, xleftmargin=1.8em, xrightmargin=0.5em, numbers=left, numberstyle=\small\ttfamily\bf, frame=single, breaklines=true, breakatwhitespace=true, breakindent=0pt, emph={}, emphstyle=\color{red}, basicstyle=\small\ttfamily, columns=fullflexible, showstringspaces=false, commentstyle=\color{gray}\upshape}

\begin{document}
 XSLT listing:
 \begin{lstlisting}[style=listXML]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     version="1.0">
<!-- ======= Processing Root tag =======   -->
<xsl:template match="Root">
    <TeXML>
      <TeXML escape="0">
                \documentclass[14pt]{extarticle}
                \usepackage[T2A]{fontenc}
                \usepackage{pscyr}
                \usepackage[cp1251]{inputenc}
                \usepackage[english,russian]{babel}
                \usepackage{longtable}
                \usepackage[a4paper, left=30mm, right=15mm, top=20mm, bottom=20mm]{geometry}
                \usepackage{indentfirst}
                \usepackage{fixltx2e}
         </TeXML>
      <env name="document">
        <cmd name="clearpage"/>
        <xsl:apply-templates select="BasicInfo | StatusInfo"/>
      </env>
    </TeXML>
  </xsl:template>
  <!-- ======= Types of opening ====== -->
  <xsl:template match="ConsDescr">
    <cmd name="par"/>
    <cmd name="textbf">
....
\end{lstlisting}
 \end{document}

Best Answer

The problem is that the following two lines in your \lstdefinelanguage{XML} are in conflict:

morestring=[s]{>}{<},
morecomment=[s][\color{orange}]{<!--}{-->},

In addition, doesn't \lstdefinelanguage{XML} redefine the XML language settings, thereby losing a lot of configs? One of the most noticeable drawbacks is that you can no longer access tagstyle, while the identifierstyle setting would highlight both tag names and contents (the embedded LaTeX code).

Would it be feasible for you to move all the settings into \lstdefinestyle instead, dropping morestring=[s]{>}{<}, and using tagstyle in place of identifierstyle?

\lstdefinestyle{listXML}{language=XML, extendedchars=true,  belowcaptionskip=5pt, xleftmargin=1.8em, xrightmargin=0.5em, numbers=left, numberstyle=\small\ttfamily\bf, frame=single, breaklines=true, breakatwhitespace=true, breakindent=0pt, emph={}, emphstyle=\color{red}, basicstyle=\small\ttfamily, columns=fullflexible, showstringspaces=false, commentstyle=\color{gray}\upshape,
morestring=[b]",
morecomment=[s]{<?}{?>},
morecomment=[s][\color{orange}]{<!--}{-->},
keywordstyle=\color{cyan},
stringstyle=\color{black},
tagstyle=\color{darkblue},
morekeywords={xmlns,version,type}
}

This is the output I got:

enter image description here