62

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?

1
  • minted might have better support?
    – Seamus
    Jul 25, 2011 at 14:46

5 Answers 5

27

XML language has very limited support for keywords. You can define more keywords yourself:

\lstset{
  language=XML,
  morekeywords={encoding,
    xs:schema,xs:element,xs:complexType,xs:sequence,xs:attribute}
}
70

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.

1
  • 1
    I want the comments in the XML which are inside <!-- --> in gray same as <? ?>. Can you tell me how to do it?
    – JHS
    Jun 9, 2013 at 13:42
25

Here's another style which is rather hacky and stingy about additional whitespace because of the attribute delimiter that starts with a space:

\definecolor{maroon}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\lstdefinelanguage{XML}
{
  basicstyle=\ttfamily,
  morestring=[s]{"}{"},
  morecomment=[s]{?}{?},
  morecomment=[s]{!--}{--},
  commentstyle=\color{darkgreen},
  moredelim=[s][\color{black}]{>}{<},
  moredelim=[s][\color{red}]{\ }{=},
  stringstyle=\color{blue},
  identifierstyle=\color{maroon}
}

Example output:

Screenshot

3
  • 1
    Finally, a style that really produces good XML formatting (for the documents I tested at least). I modified it a bit to get (almost) the same formatting as Firefox when it displays an XML file. \lstdefinelanguage{XML}{ columns=fullflexible, basicstyle=\rmfamily, commentstyle=\ttfamily\itshape\color{green!50!black}, morestring=[s]{"}{"}, morecomment=[s]{?}{?}, morecomment=[s]{!--}{--}, morecomment=[s]{!DOCTYPE}{]}, moredelim=[s][\color{black}]{>}{<}, moredelim=[s][\bfseries\color{black}]{\ }{=}, stringstyle=\color{blue}, identifierstyle=\bfseries\color{violet} }
    – alfC
    Jan 4, 2016 at 4:36
  • How can this work if you have <!DOCTYPE html> on the first line?
    – kojow7
    Aug 16, 2017 at 22:34
  • @kojow7 That is not XML, you would need to introduce additional delimiter rules.
    – H.B.
    Aug 28, 2017 at 19:10
19

A synthesis of the previous answers that works best for me, especially w.r.t. highlighting the angle brackets. Using SVG Color names (svgnames):

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

The order of the \more... commands seems to be important. Note that you need a font that supports bold-face to be Using \ttfamily with \bfseries (or how to enable bold in fixed-width font). Code is at GitHub.

Example output

2
  • You forget the comments: morecomment=[s]{<!--}{-->},. Oct 23, 2013 at 16:01
  • 2
    one improvement: adding alsoletter=-: will allow the semicolon in attribute names (such as xmlns:xs) to be coloured and similarly for attribute names with a dash, e.g. list-type. `
    – greyshade
    Sep 2, 2014 at 9:34
2

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
}
2
  • 1
    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, 2012 at 21:53
  • I needed to change darkblue to DarkBlue when using svgnames
    – m13r
    May 2, 2016 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .