Fork me on GitHub

Does anyone know of a tool like but in Java or Clojure? I want to be able to "normalize" svg drawings into some sort of common denominator, e.g. apply transformations to paths. There's no dom or gui involved, so just xml in and xml out


This is an example of some of the calculations that need to be done. From what I remember, I don't think xslt can do this, right? It's been years since I've worked with xslt


There's quite a lot behind that SO link so I'm not sure what exactly you mean. Do you mean transforming matrix(...)? You could do it with XPath 2.0, at least. (Shameless plug:


I mean I need to be able to do something like:

# cat example.svg
<g transform="matrix(0.443,0.896,-0.896,0.443,589.739,-373.223)">
    <path d="M486,313s27-9,43-29l26,4,1,23-22,5s-25-6-48-3z" />
# svgo example.svg -o -
<g><path d="M524.589 200.892s20.025 20.205 45.033 25.681l7.934 25.068-20.165 11.085-14.226-17.497s-5.699-25.058-18.576-44.337z"/></g>
according to the calculation rules in the stackoverflow link


Thanks for the pointer to sigel. This looks like it could be useful for working with the normalized svg


Right, I won't delve into the specifics of the calculation rules, but here's an example stylesheet that parses the matrix(...) values as floats and multiplies them by four:

<xsl:stylesheet version="3.0" xmlns:xsl="" xmlns:xs="">
    <xsl:template match="g">
        <xsl:variable name="m"
            for-each(tokenize(substring-before(substring-after(@transform, '('), ')'), ','), xs:float#1)
          " as="xs:float*"/>

        <xsl:for-each select="$m">
          <m><xsl:value-of select=". * 4"/></m>


Given that input, it produces:

λ saxon -xsl:stylesheet.xsl -s:input.xml | xmllint --format -
<?xml version="1.0" encoding="UTF-8"?>
<g xmlns:xs="">


That's just to illustrate that I think you can do that with XSLT & XPath. Whether you want to is another matter, of course. 🙂


That's very interesting! Thanks for the example


A full-fledged parser for possible values of the transform attribute is another matter, of course. If that's mainly what you need to do, XSLT probably isn't very useful. Perhaps you could use XPath to reach into the values, hand them to a Clojure function that parses and transforms them, then sticks them into an output XML tree. Or something along those lines.


Right, I see how xslt and xpath could be very helpful if I choose to roll my own. I hadn't thought to use them this way. Thanks for the suggestion!


Sure. An additional thing to consider is using a combination of XSLT and Java/Clojure using integrated extension functions: Essentially, you can define a Clojure function you call from your XSLT stylesheet.


That is, you could write the attribute value parser in Clojure (maybe with Instaparse?) and use XSLT for transforming the XML tree.