0

I have a SVG file, which is an XML document:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
  <defs
     id="defs2">
  <!-- a lot of stuff> </defs>
  <!-- more stuff-->
</svg>

I want to add the svg: prefix to all the tags corresponding to the default namespace in order to get next output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg:svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
  <svg:defs
     id="defs2">
  <!-- a lot of stuff> </svg:defs>
  <!-- more stuff-->
</svg:svg>

I am sure this is possible in one or few command lines from the shell using xmllint and/or xmlstarlet but I can't manage it.

0

Using xmlstarletin a particularly brutal fashion (I await the proper xpath)

for x in $(xmlstarlet sel -t -m "//*" -n -v "name()" file1.xml | sort | uniq); do 
    xmlstarlet ed -r "//svg:$x" -v "svg:$x" file1.xml > tmp.xml;
    mv tmp.xml file1.xml;
done

Since you have declared the namespace svg already, you need to invoke it in the xpath to change the literal value of the node names.

| improve this answer | |
  • what is the command xml you use on the second line? – Pierre François Mar 20 at 19:48
  • the xml command is just an alias for xmlstarlet. Same tool. – bu5hman Mar 20 at 22:39
  • OK. In my configuration, the alias xml is not available by default. So I will write xmlstarlet instead. – Pierre François Mar 21 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.