Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Working with Inkscape and XML in SVG Files

19 Mar 2016

The beauty of SVG lies in the fact it's structured in XML that's rather easy to work with. The beauty of Inkscape lies in the fact that it keeps it this way.

1 The XML Editor

The Inkscape developers immediately saw the potential in letting you work graphically as well as directly editing the underlying XML. This is why they provided the Edit → XML Editor. It's not exactly a pleasant tool to work with, however, and it suffers from a number of bugs. For instance, be careful when moving objects. The wrong object gets moved, sometimes.

Another approach is to keep an editor opened on the SVG file at all time. A good editor will let you find your objects quickly and changing their order and attributes easily. Inkscape interacts well with other programs editing the SVG it stores its drawings in. But make sure you don't accidentally overwrite with your editor changes you made with Inkscape in the meantime, and that you don't overwrite with Inkscape changes you made with your editor in the meantime. In Vim, the :e! command will come in handy to force reload the file (you'll get a helpful warning if you try to overwrite changes coming from elsewhere anyway). Similarly, Inkscape provides the File → Revert operation which will become so handy you might as well learn its shortcut: AltF V.

2 Hiding and Locking Objects

Even though the Object → Object Properties can let you hide/lock objects and the Edit → XML Editor appears to be the only graphical tool that can let you unhide/unlock them, you're much better off creating a layer for hidden/locked objects. The approach of using the editor of your choice to change these attributes and toggle object visibility and locking is of course another solid alternative.

3 Vacuum Cleaning and Dangling Defs

File → Vacuum Defs offers to remove unused definitions, e.g. filters. It doesn't always remove everything, however, but a very effective way of tracking unused definitions is to simply open the SVG file in an editor and look it all up yourself.

Occasionally, you'll also have the case of objects using defs which don't exist anymore. In effect, you get to think an object is free from any filter and will render as vectorised in PDF but it will still mysteriously appear bitmapped. Use your editor to identify the object in the tree and remove the non-existing filters they refer to.

4 Using Git to Keep Your Changes Non-Destructive

Storing data in a human-readable format such as XML (for some definition of “readable”) doesn't only have the advantage that you can work with it with the editor of your choice. You can use any tool dealing with text: grep for seeking strings, sed and awk for changing your drawings automatically, and version control systems such as git for tracking changes.

With git, you will be able to conveniently save in-place incremental changes to your work, tagging them by version if needs be, with the confidence that you'll always be able to rewind in time at any moment if something goes pear-shaped. You'll be able to inspect differences with git diff and git log -p. You'll be able to run several experiments in parallel with git branch and merge back the successful ones into your main line of work. It's a great tool to add to your workflow.

5 Unwanted Borders

If you notice unwanted borders in exported drawings, they may come from styles which are mistakenly applied to a layer rather than actual objects. This is a user mistake which surprisingly appears to be rather easy to make. But the fix is an easy one: simply edit the XML to remove the styles from there.

6 References