2012-05-08 151 views
2

我使用Inkscape生成.svg图像(这是一个xml文件)。我将节点的ID设置为'mount-arm-r'。我想从该元素中读取属性'x','y'。我似乎无法选择rect元素。通过ID选择元素,以.svg xml

python:我试过XPath,或者一次手动深度一次。我想,我想:

def parse_xml(): 
    tree = ElementTree() 
    tree.parse("torso-human.svg")       
    for node in tree.findall('.//rect'): print 'n=', node 

XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!-- Created with Inkscape (http://www.inkscape.org/) --> 

<svg 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:cc="http://creativecommons.org/ns#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:svg="http://www.w3.org/2000/svg" 
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" 
    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" 
    width="125.39109" 
    height="217.70497" 
    id="svg3136" 
    version="1.1" 
    inkscape:version="0.48.1 " 
    sodipodi:docname="torso-human.svg"> 
    <defs 
    id="defs3138" /> 
    <sodipodi:namedview 
    id="base" 
    pagecolor="#ffffff" 
    bordercolor="#666666" 
    borderopacity="1.0" 
    inkscape:pageopacity="0.0" 
    inkscape:pageshadow="2" 
    inkscape:zoom="0.7" 
    inkscape:cx="134.17406" 
    inkscape:cy="65.046531" 
    inkscape:document-units="px" 
    inkscape:current-layer="layer1" 
    showgrid="false" 
    fit-margin-top="0" 
    fit-margin-left="0" 
    fit-margin-right="0" 
    fit-margin-bottom="0" 
    inkscape:window-width="678" 
    inkscape:window-height="714" 
    inkscape:window-x="149" 
    inkscape:window-y="198" 
    inkscape:window-maximized="0" /> 
    <metadata 
    id="metadata3141"> 
    <rdf:RDF> 
     <cc:Work 
     rdf:about=""> 
     <dc:format>image/svg+xml</dc:format> 
     <dc:type 
      rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> 
     <dc:title></dc:title> 
     </cc:Work> 
    </rdf:RDF> 
    </metadata> 
    <g 
    inkscape:label="Layer 1" 
    inkscape:groupmode="layer" 
    id="layer1" 
    transform="translate(-94.4375,-394.94334)"> 
    <path 
     style="fill:#f7b42a;fill-opacity:1;stroke:#000000;stroke-width:6;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" 
     d="m 123.77824,405.89569 c 10.24991,-3.38432 20.79684,-5.42837 31.38251,-7.00357 10.08459,-3.35769 18.78867,2.77879 26.91852,7.6971 11.31402,4.36385 16.77569,16.06584 23.028,25.60256 4.24045,8.35939 4.95844,18.18912 6.47883,27.32923 0.81646,12.84443 3.26716,25.52723 3.78171,38.41123 0.89978,12.5961 -0.39774,25.23801 1.24629,37.81448 1.02427,12.22291 -1.40473,24.93719 -8.43715,35.07825 -7.47966,16.13252 -19.54923,31.09303 -36.14012,38.36243 -9.64999,2.44233 -17.82857,-5.59005 -27.35583,-5.23341 -14.78262,-5.55 -28.81674,-15.655 -35.88284,-30.0959 -5.41188,-15.13741 -10.887811,-30.71753 -11.350781,-46.928 3.049751,-10.81513 3.565751,-21.8035 1.94578,-33.0368 1.513801,-9.31727 -2.45929,-18.6844 -0.0771,-27.86458 4.302571,-16.70927 8.728671,-33.70461 17.388141,-48.73973 2.49669,-3.69472 5.83731,-6.99007 7.074,-11.39329 z" 
     id="path3757" 
     inkscape:connector-curvature="0" /> 
    <rect 
     style="fill:#ff0000;fill-opacity:1;stroke:#aa0000;stroke-width:2.06586957;stroke-linecap:square;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" 
     id="mount-arm-r" 
     width="24.593687" 
     height="4.9187374" 
     x="279.40387" 
     y="403.92834" 
     transform="matrix(0.96592583,0.25881905,-0.25881905,0.96592583,0,0)" 
     ry="1.8705337" /> 
    <rect 
     ry="2.0732613" 
     transform="matrix(0.14071606,0.99004999,-0.99004999,0.14071606,0,0)" 
     y="-109.61398" 
     x="607.01672" 
     height="5.4518275" 
     width="27.259138" 
     id="mount-leg-l" 
     style="fill:#ff0000;fill-opacity:1;stroke:#aa0000;stroke-width:2.2897675;stroke-linecap:square;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" 
     inkscape:transform-center-x="-10.607363" 
     inkscape:transform-center-y="-7.3172785" /> 
    <rect 
     inkscape:transform-center-y="-4.1491271" 
     inkscape:transform-center-x="-9.047332" 
     style="fill:#ff0000;fill-opacity:1;stroke:#aa0000;stroke-width:1.76860404;stroke-linecap:square;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" 
     id="mount-leg-r" 
     width="21.05481" 
     height="4.2109618" 
     x="585.27789" 
     y="-149.50224" 
     transform="matrix(-0.03269097,0.99946551,-0.99946551,-0.03269097,0,0)" 
     ry="1.6013756" /> 
    </g> 
</svg> 

回答

5

你忘了SVG的命名空间,这是根元素(xmlns="http://www.w3.org/2000/svg")上宣布。因此,你想是这样的:

SVG_NS = "http://www.w3.org/2000/svg" 

def parse_xml(): 
    tree = ElementTree() 
    tree.parse("torso-human.svg")       
    for node in tree.findall('.//{%s}rect' % SVG_NS): 
     print 'n=', node 

一些更多的详情,请参阅http://effbot.org/zone/element-namespaces.htm