2013-05-27 6 views
1

Je manipule des fichiers SVG dans ElementTree. Compte tenu du fichier test.svgAjouter un attribut préfixé dans ElementTree

<?xml version='1.0' encoding='utf-8'?> 
<svg 
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink"> 
</svg> 

J'ai essayé de créer un élément avec préfixe spécifique

import xml.etree.ElementTree as ET 
ET.register_namespace("", "http://www.w3.org/2000/svg") 
tree = ET.parse('test.svg') 
tree.getroot().set("xmlns:xlink", "http://www.w3.org/1999/xlink") 
link = ET.fromstring('<a xlink:href="http://www.mysite.com/"></a>') 
tree.write('worldMap/test_out.svg', encoding = 'utf-8', xml_declaration = True) 

mais courir dans une erreur unbound prefix. J'ai regardé this tutorial mais je n'arrive pas à voir ce qui ne va pas.

+0

Alors ce qui est la question? – enginefree

Répondre

0

Vous devez déclarer xlink également dans la chaîne que vous analysez avec fromstring:

link = ET.fromstring('<a xmlns:xlink="http://www.w3.org/1999/xlink" ' 
        'xlink:href="http://www.mysite.com/"></a>') 
+0

Parfait, merci! – u003f

Questions connexes