[Tex/LaTex] XML highlighting – different style for attributes

highlightinglistingsxml

I need to highlight syntax in my LaTeX presentation, so I've found this answer:
XML syntax highlighting

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type},
  backgroundcolor=\color{lightgray},
  numbers=left,
  numberstyle=\footnotesize\ttfamily\color{gray},
  numbersep=0.5pt
}

Unfortunately I didn't find any way, how to specify extra style for text in attributes.

For example:

<xml>
    <person age="22" sex="female">Ann</person>
</xml>

It the case above, Ann and 22, female would have the same style… How to separate styles for those two things?

Best Answer

When you add the color package and change

morestring=[b]",

into

morestring=[b][\color{red}]",

the attributes will color red.

This will color the attributes red in your xml listings.

\begin{lstlisting}[language=xml, frame=single]
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{lstlisting}

enter image description here

\documentclass{article}

\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{rgb}{.7,.7,.7}
\definecolor{gray}{rgb}{.4,.4,.4}
\definecolor{darkblue}{rgb}{0,0,.3}
\begin{document}

\begin{lstlisting}[language=xml, frame=single]
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{lstlisting}

 \lstdefinelanguage{XML}
{
  morestring=[b][\color{red}]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type},
  backgroundcolor=\color{lightgray},
  numbers=left,
  numberstyle=\footnotesize\ttfamily\color{gray},
  numbersep=0.5pt
}

\begin{lstlisting}[language=xml, frame=single]
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{lstlisting}

\end{document}




 \lstdefinelanguage{XML}
{
  morestring=[b][\color{red}]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type},
  backgroundcolor=\color{lightgray},
  numbers=left,
  numberstyle=\footnotesize\ttfamily\color{gray},
  numbersep=0.5pt
}