# How to change color of html tags in lstset?

I have a piece of code: "></li></a><script>alert(123);</script> that I would like to color using \lstset (i.e => the HTML tags like "li" "a" "script" would be dkblue color). I'm using "listings" package and \lstset extension as this:

\lstset{
language        = html,
basicstyle      = \small\ttfamily,
keywordstyle    = \color{dkblue},
stringstyle     = \color{red},
identifierstyle = \color{dkgreen},
emph            =[1]{html},
emphstyle       =[1]\color{black},
emph            =[2]{if,and,or,else},
emphstyle       =[2]\color{dkyellow}}


The problem is that putting "> before the rest of the code prevent LaTeX to interpret the code as HTML. Someone as a solution ?

Your code snippet is correctly colored: it starts with a " and therefore it is a string.

A quick and dirty (actually not very quick yet considerably dirty) solution is to copy the html driver from lstlisting without the string delimiter, and add your own preferences:

\documentclass{article}
\usepackage{listings,xcolor}
\definecolor{dkgreen}{rgb}{0,.6,0}
\definecolor{dkblue}{rgb}{0,0,.6}
\definecolor{dkyellow}{cmyk}{0,0,.8,.3}
\lstdefinelanguage{nohtml}{
BDO,BIG,BLOCKQUOTE,BODY,BR,BUTTON,CAPTION,CENTER,CITE,CODE,COL,%
COLGROUP,DD,DEL,DFN,DIR,DIV,DL,DOCTYPE,DT,EM,FIELDSET,FONT,FORM,%
NOFRAMES,NOSCRIPT,OBJECT,OPTGROUP,OPTION,P,PARAM,PLAINTEXT,PRE,%
OL,Q,S,SAMP,SCRIPT,SELECT,SMALL,SPAN,STRIKE,STRING,STRONG,STYLE,%
VAR,XMP,%
code,codebase,codetype,color,cols,colspan,content,coords,data,%
longdesc,marginwidth,marginheight,maxlength,media,method,multiple,%
selected,shape,size,src,standby,style,tabindex,text,title,type,%
tag=**[s]<>,%
sensitive=f,%
basicstyle      = \small\ttfamily,
keywordstyle    = \color{dkblue},
stringstyle     = \color{red},
identifierstyle = \color{dkgreen},
emph            =[1]{html},
emphstyle       =[1]\color{black},
emph            =[2]{if,and,or,else},
emphstyle       =[2]\color{dkyellow}
}
\begin{document}
\begin{lstlisting}[language=nohtml]

An advantage of this solution is that for other, well-formed html you can still use the default html definition (with strings).