0

Using Apache Batik to create SVG files, the version always comes out as "1.0" (seems to be hard coded) but Batik says it implements 1.1. How do I change the version, is it as simple as setting the attribute manually on the <svg> element or would that have other consequences? Or does the SVG version not matter? I used an SVG validator and it recommended setting the version to 1.1 or omitting it.

import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.w3c.dom.*;
import org.w3c.dom.svg.SVGDocument;

public class Test {
    public static void main(String[] args) throws IOException, TransformerException {
        DOMImplementation imp = SVGDOMImplementation.getDOMImplementation();
        SVGDocument doc = (SVGDocument) imp.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
        TransformerFactory tf = TransformerFactory.newDefaultInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(new DOMSource(doc), new StreamResult(System.out));
    }
}

output

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" 
     xmlns:xlink="http://www.w3.org/1999/xlink" 
     contentScriptType="text/ecmascript" 
     zoomAndPan="magnify" 
     contentStyleType="text/css" 
     preserveAspectRatio="xMidYMid meet" 
     version="1.0"/>
Peter Hull
  • 6,683
  • 4
  • 39
  • 48
  • 2
    See https://stackoverflow.com/questions/18467982/are-svg-parameters-such-as-xmlns-and-version-needed/18468348#18468348 – Robert Longson Jul 27 '23 at 08:58
  • If I understand correctly the answer suggests it doesn't matter in practice, is that right? However I'd like to do it properly if possible! – Peter Hull Jul 27 '23 at 09:31
  • If you use `getAttributes()` on `doc` and `getNamedItemNS()` on the NamedNodeMap, can you then debug and modify the version attribute? – chrwahl Jul 29 '23 at 16:37
  • @chrwahl l at the moment I'm just using `setAttribute` on the `` element and it seems to work – Peter Hull Jul 31 '23 at 08:12
  • @PeterHull ok. That makes sense. But we can agree that it sounds a bit hacky. – chrwahl Jul 31 '23 at 14:48
  • @chrwahl I agree but it's only a little bit hacky - after all the document element is just a DOM element and it's 'acceptable' to get and set an element's attributes I believe. – Peter Hull Aug 02 '23 at 13:03

0 Answers0