extract entire elements from xml which are matched with specific text

时间:2017-06-15 10:04:29

标签: python xml xpath lxml

I'm a student and newbie for XML and Python. Currently, I have a really huge XML file that I need to split it into a small XML file, which means to extract some of specific the elements out into a new XML file. The elements I need to extract contain with selected text. Bellow is an example XML:

...
<ns:FeatureCollection>
    <ns:FeatureMember>
        <ns:node ns:id="n1">
            <ns:name>Node1</ns:name>
            <ns:position>1000.00 1000.00 0.0</ns:position>
            <ns:haspoint Objectclass="point" xlink:href="p1"/>
            <ns:haspoint Objectclass="point" xlink:href="p2"/>
        </ns:node>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:node ns:id="n2">
            <ns:name>Node2</ns:name>
            <ns:position>2000.00 2000.00 0.0</ns:position>
            <ns:haspoint Objectclass="point" xlink:href="p3"/>
        </ns:node>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:node ns:id="n3">
            <ns:name>Node3</ns:name>
            <ns:position>3000.00 3000.00 0.0</ns:position>
            <ns:haspoint Objectclass="point" xlink:href="p4"/>
        </ns:node>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p1">
            <ns:name>Point1</ns:name>
            <ns:innode Objectclass="node" xlink:href="n1"/>
        </ns:point>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p2">
            <ns:name>Point2</ns:name>
            <ns:innode Objectclass="node" xlink:href="n1"/>
        </ns:point>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p3">
            <ns:name>Point3</ns:name>
            <ns:innode Objectclass="node" xlink:href="n2"/>
        </ns:point>        
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p4">
            <ns:name>Point4</ns:name>
            <ns:innode Objectclass="node" xlink:href="n3"/>
        </ns:point>
    </ns:FeatureMember>
</ns:FeatureCollection>

Now I have the specific node positions: 1000.00 1000.00 0.0 and 2000.00 2000.00 0.0, I want to extract all the elements of nodes and their relevant elements of points out and create a new XML file. The new file should be like:

...
<ns:FeatureCollection>
    <ns:FeatureMember>
        <ns:node ns:id="n1">
            <ns:name>Node1</ns:name>
            <ns:position>1000.00 1000.00 0.0</ns:position>
            <ns:haspoint Objectclass="point" xlink:href="p1"/>
            <ns:haspoint Objectclass="point" xlink:href="p2"/>
        </ns:node>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:node ns:id="n2">
            <ns:name>Node2</ns:name>
            <ns:position>2000.00 2000.00 0.0</ns:position>
            <ns:haspoint Objectclass="point" xlink:href="p3"/>
        </ns:node>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p1">
            <ns:name>Point1</ns:name>
            <ns:innode Objectclass="node" xlink:href="n1"/>
        </ns:point>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p2">
            <ns:name>Point2</ns:name>
            <ns:innode Objectclass="node" xlink:href="n1"/>
        </ns:point>
    </ns:FeatureMember>
    <ns:FeatureMember>
        <ns:point ns:id="p3">
            <ns:name>Point3</ns:name>
            <ns:innode Objectclass="node" xlink:href="n2"/>
        </ns:point>        
    </ns:FeatureMember>
</ns:FeatureCollection>

How can I do with XPath and Python? Thank you very much!

0 个答案:

没有答案