Skip to content Skip to sidebar Skip to footer

How To Change Different Hierarchy Tags With Lxml?

I want to change all tags names

to using lxml in python. The problem is I'm able to change the outer concept tag's

tag and the first inner con

Solution 1:

I think you're overcomplicating it. Just find all of the p elements (with .xpath() or .findall()) and change the value of the .tag property...

from lxml import etree

tree = etree.parse("73-20.xml")

for p in tree.findall(".//p"):
    p.tag = "paragraph"

tree.write("FerNewtags.xml")

Output (FerNewtags.xml)

<dita>
    <topic id="id15CDB0PL09E">
        <title id="id15CDB0R0VYB"><?FM MARKER [Header/Footer $1] All?>Control
        </title>
        <shortdesc>CONTROL</shortdesc>
        <concept id="id15CDB0Q0Q4G">
            <title id="id15CDB0R0VHA">General
            </title>
            <conbody>
                <paragraph>This section
                </paragraph>
            </conbody>
            <concept id="id156F7H00GIE">
                <title id="id15CDB0R0V1W">System
                </title>
                <conbody>
                    <paragraph>Engine
                    </paragraph>
                    <paragraph>The ECU
                    </paragraph>
                    <paragraph>The aircraft
                    </paragraph>
                    <paragraph>The system
                    </paragraph>
               </conbody>
            </concept>
        </concept>
    </topic>
</dita>

Post a Comment for "How To Change Different Hierarchy Tags With Lxml?"