【问题标题】:Suppressing namespace prefix in XML file抑制 XML 文件中的命名空间前缀
【发布时间】:2021-09-28 11:25:50
【问题描述】:

输入文件:

<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="json" ContentType=""/>
    <Override PartName="/Version" ContentType=""/>
</Types>

我得到的输出:

<?xml version='1.0' encoding='utf-8'?>
<xmlns:Types xmlns:xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <xmlns:Default Extension="json" ContentType=""/>
    <xmlns:Override PartName="/Version" ContentType=""/>
    <xmlns:Default Extension="png" ContentType=""/>
</xmlns:Types>

所需输出:

<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="json" ContentType=""/>
    <Override PartName="/Version" ContentType=""/>
    <Default Extension="png" ContentType=""/>
</Types>

我使用以下行添加了具有扩展属性的默认标签,其中“png”作为值。添加上面的标签命名空间被添加到每个标签。我是 Python,XML 处理的新手。我试过这个:

import xml.etree.ElementTree as ET
def formatxml():
    dest="input.xml"
    tree = ET.parse(dest)
    root = tree.getroot()
    ET.register_namespace('xmlns',"http://schemas.openxmlformats.org/package/2006/content-types")
    child=ET.SubElement(root,"{http://schemas.openxmlformats.org/package/2006/content- types}Default")
    child.set('Extension',"png")
    child.set('ContentType',"")
    tree.write(dest,xml_declaration=True,encoding='utf-8',method="xml")

【问题讨论】:

  • 为什么不使用 XSLT 来完成这样的任务?
  • 如果可以的话,使用 lxml 代替 elementttree。

标签: python xml elementtree xml-namespaces


【解决方案1】:

register_namespace() 设置序列化 XML 时使用的前缀。由于您不需要任何前缀,因此请使用空字符串。

你需要做的就是改变

ET.register_namespace('xmlns',"http://schemas.openxmlformats.org/package/2006/content-types")` 

ET.register_namespace('',"http://schemas.openxmlformats.org/package/2006/content-types")

【讨论】:

    【解决方案2】:

    这里是如何使用 XSLT 来完成任务。

    XSLT 遵循所谓的 Identity Transform 模式。

    输入 XML

    <?xml version="1.0" encoding="utf-8"?>
    <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
        <Default Extension="json" ContentType=""/>
        <Override PartName="/Version" ContentType=""/>
    </Types>
    

    XSLT

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://schemas.openxmlformats.org/package/2006/content-types" exclude-result-prefixes="ns1">
        <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="ns1:Override">
            <xsl:copy-of select="."/>
            <Default Extension="png" ContentType=""/>
        </xsl:template>
    </xsl:stylesheet>
    

    输出 XML

    <?xml version='1.0' encoding='utf-8' ?>
    <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
      <Default Extension="json" ContentType=""/>
      <Override PartName="/Version" ContentType=""/>
      <Default Extension="png" ContentType=""/>
    </Types>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多