# Listing: XML keywords, strings, identifiers not recognized

I'd like to implement some XML code in my document. However, the syntax of my listings doesn't seem to be acknowledged. So my output looks like this right now:

Obviously, that doesn't make any sense. I tried to redefine the keywords, using "more keywords" but that didn't cause the desired effect. I'm using the listings package. Could anyone help me with a correct syntax definition for my XML Listings? (It's SAPUI5 identifiers, that's why some of them are not "common")

Here's the necessary code to reproduce my document:

\documentclass[11pt, a4paper, toc = bibliography, toc = listof,twoside,table,titlepage]{scrartcl}
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstdefinelanguage{JavaScript}{
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
keywordstyle=\color{blue}\bfseries,
ndkeywords={class, export, boolean, throw, implements, import, this},
ndkeywordstyle=\color{blue}\bfseries,
identifierstyle=\color{black},
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
stringstyle=\color{red}\ttfamily,
morestring=[b]',
morestring=[b]"
}
\lstset{frame=tb,
language=JavaScript,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=left,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}

\begin{document}
\begin{lstlisting}[caption={Aggregation Binding über die View} ,captionpos=b, language=XML,  label=lst:aggregationBinding]
<Table id="table" items="{Students}>
<columns>
<Column id="matrikelnummer">
<Text text="Matrikelnummer:"/>
</Column>
<Column id="name">
<Text text="Name:"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectIdentifier text="{Matrikelnummer}"/>
<ObjectIdentifier text="{Name}"/>
</cells>
</ColumnListItem>
</items>
</Table>
\end{lstlisting}
\end{document}

• Excuse me, but your document is created with LaTeX? If is possible can you add, please? – Sebastiano Dec 7 '18 at 10:49
• You may want to look into the Pygments-based syntax highlighting provided by the minted package. Because it uses Python and not TeX its highlighting capabilities can be more advanced in some areas (and may be easier to tweak). Possibly related: tex.stackexchange.com/q/10255/35864 – moewe Dec 7 '18 at 10:57
• sorry for not providing everything necessary! I added my code now (I hope that'S everything relevant)! @Sebastiano – sonja Dec 7 '18 at 11:10
• @moewe is this sufficient as a MWE ? :) And I'll look into your reference.. but I would think that it should be possible with the listings package.. – sonja Dec 7 '18 at 11:10
• Almost: It is missing a \documentclass. Ideally we can just paste and run the example without adding any additional code (see the link I gave above). You may also want to remove the excessive indent of the lines (probably introduced by the code highlighting). I'm a bit confused that all your \lst... settings are for JavaScript, but your question is about XML. Do you have a specific setup for XML already? (And I'm still not sure on the expected/desired output.) – moewe Dec 7 '18 at 11:13

The main reason why the syntax highlighting is off is the missing " in items="{Students}. This throws listings' string highlighting off quite a bit (but I assume an XML parser would also complain, so ...).

With the " added in the output is already very sensible, but the presets for XML in listings don't know a great deal of predefined keywords. So if you want to colour things like text, you need to add them yourself. As far as I can see the easiest way to extend an existing language is by defining a new style (see Extend a language with additional keywords?).

\documentclass[11pt, a4paper]{scrartcl}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=left,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}

\lstdefinestyle{XML}{
language=XML,
morekeywords={text,id},
}

\begin{document}
\begin{lstlisting}[caption={Aggregation Binding über die View} ,captionpos=b, style=XML,  label=lst:aggregationBinding]
<Table id="table" items="{Students}">
<columns>
<Column id="matrikelnummer">
<Text text="Matrikelnummer:"/>
</Column>
<Column id="name">
<Text text="Name:"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectIdentifier text="{Matrikelnummer}"/>
<ObjectIdentifier text="{Name}"/>
</cells>
</ColumnListItem>
</items>
</Table>
\end{lstlisting}
\end{document}


gives

In XML syntax highlighting you may find more extended styles for XML highlighting.

If you need more complex rules or hope for a more automatic solution, maybe minted is worth a look. minted is based on Pygments and can make use of the power of Python to highlight your code.