<p>考虑一下<a href="https://www.w3.org/Style/XSL/" rel="nofollow noreferrer">XSLT</a>,这是一种专门用来转换XML文件的语言,比如删除名称空间。Python的第三方模块<code>lxml</code>,可以运行xslt1.0脚本。因为XSLT脚本是XML文件,所以可以像任何XML一样从文件或字符串进行解析。不需要循环或条件<code>if</code>逻辑。此外,您还可以在其他语言(PHP、Java、C#等)中使用这个XSLT脚本</p>
<p><strong>XSLT</strong><em>(另存为.xsl文件,在Python中引用)</em></p>
<pre><code><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<! IDENTITY TRANSFROM: COPY DOC AS IS >
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<! REMOVE NAMESPACE PREFIXES, ADD DOC NAMESPACE >
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="urn:ietf:params:xml:ns:epp-1.0">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
</code></pre>
<p><strong>Python</strong></p>
^{pr2}$
<p><strong>输出</strong></p>
<pre><code><?xml version="1.0"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<check>
<check>
<id>ex61-irnic</id>
<id>ex999-irnic</id>
<authInfo>
<pw>1487441516170712</pw>
</authInfo>
</check>
</check>
<clTRID>TEST-12345</clTRID>
</command>
</epp>
</code></pre>