[[!meta title="eXtensible Style Language Transforms"]]
Often data is stored in
XML files must be
massaged into other formats (e.g. [[DocBook to roff|DocBook 5]]).
There are well developed procedures for defining such transformations
([XSLT][]) and a number of tools to apply them (e.g. [xsltproc][]).
Besides the [W3 tutorial][w3], there is also a nice
[introduction][intro] by Paul Grosso and Norman Walsh. I've copied a
simple [[example|chapter]] from this intro and also included a
[[slightly more complicated setup|code]] for generating online help
for a list of macros.
XSLT is also useful for standardizing XML content. For example, I was
recently trying to compare to [[Gramps]] XML files, to see what had
changed between two database backups. Unfortunately, the backup XML
was not sorted by `id`, so there were many diff chunks due to node
shuffling that didn't represent any useful information. With the
following XSLT:
With the above saved as `sort-by-id.xsl`, you can sort `some.xml` using
$ xsltproc --nonet --novalid sort-by-id.xsl some.xml
You can compare two [[Gramps]] XML files with
$ diff -u <(zcat a.gramps | xsltproc --nonet --novalid sort-by-id.xsl -)
<(zcat b.gramps | xsltproc --nonet --novalid sort-by-id.xsl -) | less
Jesper Tverskov has a nice page about [the identity template and
related tricks][identity] if you want more examples of quasi-copy
transforms.
[XSLT]: http://www.w3.org/TR/xslt
[w3]: http://www.w3schools.com/xsl/
[intro]: http://nwalsh.com/docs/tutorials/xsl/xsl/slides.html
[xsltproc]: http://www.xmlsoft.org/XSLT/
[identity]: http://www.xmlplease.com/xsltidentity
[[!tag tags/tools]]
[[!tag tags/web]]