在.svg xml中按ID选择元素 [英] Selecting element by ID, in .svg xml
问题描述
我使用Inkscape生成 .svg
图像(这是一个xml文件)。我将节点的ID设置为 mount-arm-r。我想从该元素中读取属性 x, y。我似乎无法选择 rect
元素。
I generate a .svg
image (which is a xml file) using Inkscape. I set a node's ID to 'mount-arm-r'. I want to read the attributes 'x', 'y', from that element. I can't seem to select the rect
elements.
python:我尝试了XPath,或者手动选择了一个深度一个时间。我想我想:
python: I tried XPath, or manually one depth at a time. I think I want:
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>
推荐答案
您忘记了SVG名称空间,该名称空间已声明在根元素上( xmlns = http://www.w3.org/2000/svg
)。因此,您需要这样的东西:
You're forgetting the SVG namespace, which is declared on the root element (xmlns="http://www.w3.org/2000/svg"
). Hence, you want something like this:
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 一些更多的细节。
See http://effbot.org/zone/element-namespaces.htm for some more details.
这篇关于在.svg xml中按ID选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!