Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
minted might have better support? – Seamus Jul 25 '11 at 14:46

3 Answers

up vote 13 down vote accepted

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}
}
share|improve this answer

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,
  commentstyle=\color{gray}\upshape
}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}% list your attributes here
}

Here is an example of highlighted XML with the settings above:

XML syntax highlighting

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.

share|improve this answer
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},
  morekeywords={xmlns,version,type}% list your attributes here
}
share|improve this answer
I cannot see how this colorizes the missing characters. At least it did not work for me. Could you please double check if you missed something and add a screenshot please? – JJD Sep 18 '12 at 21:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.