Medusis Tech Blog

CSS2XML

On XSL list someone asked how to serialize a CSS to XML. There appears to be no ready-made solution, so I gave it a shot.

There are a couple of projects that try to parse CSS from scratch -- for instance, a node.js library that parses CSS and outputs JSON; an XSL project that parses CSS in XSLT and reinjects the results in a custom-built XSL stylesheet for further processing...

The problem with these initiatives is that they need to be actively maintained to stay current; most aren't.

CSS parsing is difficult; rather than trying to come up with your own parser, it's much much better to use an existing specialized library. CSSutils is such a library, available for Python 2.x (>=2.5) and 3.x; it's pretty complete (it even parses comments!) and active.

10 lines of Python

Using this library, it's pretty easy to produce an XML version of a given CSS; here's the Python code:

import cssutils, gnosis.xml.pickle

css = {}
sheet = cssutils.parseFile("yourcssfile.css")

for rule in sheet:
  if rule.type == rule.STYLE_RULE:
    css[rule.selectorText] = {}
    for property in rule.style:
      css[rule.selectorText][property.name] = property.value

print gnosis.xml.pickle.dumps(css)

(We create an object for each rule and property, and then serialize this object to XML. Or, to serialize to JSON instead, import json and change the last line to 'print json.dumps(css)').

Here, the CSS is taken from a local file; but cssutils is able to parse either a string, or even fetch a "live" CSS (cssutils.parseUrl deals with urllib2 directly).

Many things could be improved from this simple example, but that should get one started.

And if anyone is interested in using [a more elaborate version of] this as a service, please get in touch!

2013-04-03