Home News changing styles at runtime using XML entities

changing styles at runtime using XML entities

By Graham Asher on 07 December 2011

Wouldn't it be nice if you could make an attribute in your style sheet, like a road color or a font size, into a variable which you could change from your application? There's already a way to do this using style overrides, but here's a simpler and more efficient way.

Invent a name for your variable, like 'roadcolor' and use XML entity syntax, which means you write it as &roadcolor; - put an & in front and a ; after it. Then you can use it anywhere in your style sheet, like this:

<line width='12m,0.5pt' fill='&roadcolor;' border='dimgrey' borderWidth='8%,0.7,2'/>

To make it work, you have to give it a value. Call CMap::SetStyleSheetVariable:

my_map->SetStyleSheetVariable("roadcolor","lightgreen");

You can change the value at any time by calling CMap::SetStyleSheetVariable, then, after you have made any other changes to style sheet variables, call CMap::ClearStyleSheet to tell CartoType to reload the style sheet before the next drawing operation.