<p><strong>如果</strong>您的输入将是XML,它将如下所示:</p>
<pre><code><?xml version="1.0"?>
<root>
<AVP code="123" name="abcd">
<type>
<high/>
</type>
</AVP>
<AVP code="234" name="ukbkj">
<type>
<enum/>
</type>
<type>
<enum/>
</type>
</AVP>
</root>
</code></pre>
<p>您可以使用这个XSLT删除所有<code>AVP</code>元素。你知道吗</p>
<pre><code><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="AVP[@code='123']" />
</xsl:stylesheet>
</code></pre>
<p>使用Python应用它:</p>
<pre><code>import lxml.etree as ET
dom = ET.parse("avp.xml")
xslt = ET.parse("avp.xslt")
transform = ET.XSLT(xslt)
newdom = transform(dom)
with open("avp-out.xml", "wb") as o:
o.write(ET.tostring(newdom, pretty_print=True))
</code></pre>
<p>输出在<code>avp-out.xml</code>:</p>
<pre><code><root>
<AVP code="234" name="ukbkj">
<type>
<enum/>
</type>
<type>
<enum/>
</type>
</AVP>
</root>
</code></pre>