0

I'm trying to get a specific DOCTYPE entry in my SVG output when using Apache Batik (1.14).

A simple example looks like:

        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        String svgNS = "http://www.w3.org/2000/svg";
        DocumentType docType = domImpl.createDocumentType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd");
        Document document = domImpl.createDocument(svgNS, "svg", docType);
        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
        SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
        Writer out = new OutputStreamWriter(System.out, "UTF-8");
        svgGenerator.stream(out, true);

Which produces:

<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>

I was hoping for something more like:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

(based on the SVG 1.1 spec entry at https://www.w3.org/TR/SVG11/struct.html#NewDocument).

How can I get the desired DOCTYPE (ideally without munging the XML afterwards)?

BradHards
  • 650
  • 9
  • 27
  • 1
    Why? Everything ignores doctypes. – Robert Longson Mar 14 '22 at 12:56
  • Why? Validation and cross-domain security checking applications. – BradHards Mar 15 '22 at 09:01
  • 1
    https://www.w3.org/TR/2015/WD-SVG2-20150709/intro.html *A DTD is not provided in this specification, as the use of DTDs for validating documents is known to be problematic. In particular, DTDs do not handle namespaces gracefully and the range of constraints they can express is limited. It is recommended that authors do not include a DOCTYPE declaration in SVG documents.* – Robert Longson Mar 15 '22 at 09:06
  • Thanks - any idea why that doesn't appear in the current drafts? It looks like the DTD isn't provided, but this context doesn't seem to be there either. – BradHards Mar 15 '22 at 09:16
  • 1
    https://svgwg.org/svg2-draft/changes.html#concepts *References to the SVG DTD have been removed.* – Robert Longson Mar 15 '22 at 09:22

1 Answers1

1

Rather than struggle with Batik over DOCTYPE generation, just skip it. As Robert commented, it's generally ignored. Furthermore, using a DOCTYPE is not recommended with SVG:

In fact SVG WG members are even telling people not to use a DOCTYPE declaration in SVG 1.0 and 1.1 documents. Instead always include the version and baseProfile attributes on the root <svg> tag, and assign them appropriate values as in the following example.

<svg version="1.1"
     baseProfile="full"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:ev="http://www.w3.org/2001/xml-events">
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • OK (not what I asked for, and not what I wanted to do, but perhaps a valid alternative depending on what a validator might check). Do you have a source reference for the SVG WG claim? I note some of the SVG examples in the SVG 2.0 repo still use it (e.g. https://github.com/w3c/svgwg/blob/main/master/style/logo-PR-v.svg). – BradHards Mar 15 '22 at 09:00
  • The source is cited via a working link. I see that the target of the link has a dead link, apparently to an old Yahoo Groups malling list -- you could trying googling around given the subject and that context. – kjhughes Mar 15 '22 at 12:34