Beginner Beginner - 1 month ago 8
HTML Question

Access very first tag (xml tag) of a svg file with java

I would like to change/modify the very first tag of a svg file. The tag "< ?xml ...>"

My purpose is to modify/replace this tag to add attributes like < ?xml-stylesheet type="text/css" href="file:svg-stylesheet.css" ...

I want to add these attributes to external a css sheet for all my future elements.

Here the svg file with the original xml tag :

<?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" width="1000" height="1000" version="1.0">
.
.
.
</svg>


Here is the new svg file I want after modifying the xml tag :

<?xml-stylesheet type="text/css" href="file:svg-stylesheet.css" version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1000" height="1000" version="1.0">
.
.
.
</svg>


Is there a way to acces this very first node with dom or batik, or whatever ...?
I did not succeed in getting it by conventionnal methods such as :

doc.getFirstchild() or doc.getElementsByTagName(tagname)

Answer

Will something like that work for you?

ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet",
    "type=\"text/css\" href=\"file:svg-stylesheet.css\""
);
doc.insertBefore(pi, root);