English 中文(简体)
ID选择要素,载于:svg xml
原标题:Selecting element by ID, in .svg xml

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,或者在某个时候手工操作一个深度。 我想:

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>
最佳回答

页: 1 因此,你们想要的是:

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

See http://effbot.org/zone/element-namespaces.htm for some more details.

问题回答

或者在任何地方使用野心扫描仪<条码>查询。

tree.findall( .//{*}rect )

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




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签