views:

217

answers:

2

I'm building a set of SVG files that include an unfortunate number of hardcoded values (they must print with some elements sized in mm, while others must be scaled as a percent, and most of the values are defined relative to each other). Rather than managing those numbers by hand (heaven forbid I want to change something), I thought I might use my trusty hammer python for the task.

SVG 1.1 doesn't natively support any kind of variable scheme that would let me do what I want, and I'm not interested in introducing javascript or unstable w3c draft specs into the mix. One obvious solution is to use string formatting to read, parse, and replace variables in my SVG file. This seems like a bad idea for a larger document, but has the advantage of being simple and portable.

My second though was to investigate the available python->svg libraries. Unfortunately, it seems that the few options tend to be either too new (pySVG still has an unstable interface), too old (not updated since 2005), or abandoned. I haven't looked closely, but my sense is that the charting applications are not flexible enough to generate my documents.

The third option I came across was that of using some other drawing tool (cairo, for instance) that can be convinced to put out svg. This has the (potential) disadvantage of not natively supporting the absolute element sizes that are so important to me, but might include the ability to output PDF, which would be convenient.

I've already done the googling, so I'm looking for input from people who have used any of the methods mentioned, or who might know of some other approach. Long-term stability of whatever solution is chosen is important to me (it was the original reason for the hand-coding instead of just using illustrator).

At this point, I'm leaning towards the first solution, so recommendations on best practices for using python to parse and replace variables in XML files are welcome.

A: 

Since SVG is XML, maybe you could use XSLT to transform a source XML file containing your variables to SVG. In your XSLT style sheet, you would have templates corresponding to various elements of your SVG illustration, that change their output depending on the values found in the source XML file.

Or you could use a template SVG as the source and transform it into the final one, with the values passed as parameters to the XSLT processor.

You's either use XSLT directly, or via Python if you need some logic that's easier to perform in a traditional language.

Ölbaum
So that would be a variation of your first solution, using a solution adapted to dealing with XML and providing powerful tools to help you, such as XPath.
Ölbaum
I've done stuff in the past using XSLT, and came away with a really nasty taste in my mouth. The parsers aren't standardized as far as features go, and simple things like date transformations involved half a page of cruft. I'd like to stay away from them, but might go have another look if there's not a better option.
Paul McMillan
Though really, thinking more about it, maybe my task is simple enough that XSLT is the right way to go. It would be nice to keep the more complex languages out of the equation.
Paul McMillan
Out of curiosity, I just had a look at the Python bindings for libxslt (it's also been a while since I did something in XSLT, and now I'm using Python a lot). The second example, extfunc.py, shows how to define a Python function you can call from the XSLT transformation. That could come in handy, for example in case you'd ever need to make date transformation ;-)http://xmlsoft.org/XSLT/python.html
Ölbaum
If you're going down the XSLT path, you might want to look at lxml, the more modern and pythonic bindings for libxml2 and libxslt. http://codespeak.net/lxml/xpathxslt.html#xslt
Ned Deily
+3  A: 

A markup based templating engine, such as genshi might be useful. It would let you do most of the authoring using a SVG tool and do the customization in the template. I'd definitely prefer it to XSLT.

Ants Aasma
That's a good point. XSLT still makes me shudder. And for SVG documents, Genshi should allow me to develop with test values, and then replace them as is convenient. I'd be able to keep the templates open in my browser as I work.
Paul McMillan