使用 Python 處理 XML
移除 Namespace
def stripNamespaces(tree):
# http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
xslt_root = etree.XML('''\
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
''')
transform = etree.XSLT(xslt_root)
tree = transform(tree)
return tree
移除 comment
parser = etree.XMLParser(remove_comments=True)
tree = etree.parse(file, parser)