在python minidom中更改名称空间

时间:2012-05-02 10:03:23

标签: python xml dom minidom

我有以下xml:

<gml:FeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:aqd="http://www.exampleURI.com/AQD"
 xmlns:base="urn:x-inspire:specification:gmlas:BaseTypes:3.2"
 xmlns:gmd="http://www.isotc211.org/2005/gmd" 
 xmlns:gco="http://www.isotc211.org/2005/gco" 
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:gml="http://www.opengis.net/gml/3.2" 
 xmlns:gss="http://www.isotc211.org/2005/gss" 
 xmlns:gts="http://www.isotc211.org/2005/gts" 
 xmlns:gsr="http://www.isotc211.org/2005/gsr" 
 xmlns:ef="http://inspire.jrc.ec.europa.eu/schemas/ef/2.0"
 xmlns:base2="http://inspire.jrc.ec.europa.eu/schemas/base2/0.1"
 xmlns:om="http://www.opengis.net/om/2.0"
 xmlns:swe="http://www.opengis.net/swe/2.0" 
 xmlns:sams="http://www.opengis.net/samplingSpatial/2.0" 
 xmlns:sam="http://www.opengis.net/sampling/2.0" 
 xmlns:am="http://inspire.jrc.ec.europa.eu/schemas/am/2.0"
 xmlns:gn="urn:x-inspire:specification:gmlas:GeographicalNames:3.0" 
 xmlns:am-ru="http://inspire.jrc.ec.europa.eu/schemas/am-ru/2.0" 
 xsi:schemaLocation="http://www.exampleURI.com/AQD aqd/1.0/AQD.xsd http://www.opengis.net/gml/3.2 gml/3.2.1/gml.xsd" 
 gml:id="aqd.es.zones"
>

<!-- Responsible Party Information  -->
                <aqd:AQD_ReportingUnits>
                <am-ru:reportingAuthority>
                    <gmd:CI_ResponsibleParty>
                        <gmd:organisationName>Subdirección General de Calidad del Aire y Medio Ambiente Industrial</gmd:organisationName>
                    </gmd:CI_ResponsibleParty>
                </am-ru:reportingAuthority>
                </aqd:AQD_ReportingUnits>

当值'xsi:schemaLocation =“http://www.exampleURI.com/AQD aqd / 1.0 / AQD.xsd'出现时,我只想将'aqd / 1.0 / AQD.xsd'更改为AQD的.xsd

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这样做的方法很慢,你不需要minidom(如果文件是巨大的,以不同的方式做):

outFile = open('path/to/outfile/out_file.xml', 'w')
inFile = open('path/to/input/input_file.xml', 'r')
for line in inFile:
    if  'xsi:schemaLocation="http://www.exampleURI.com/AQD aqd/1.0/AQD.xsd' in line:
        line = line.replace('aqd/1.0/AQD.xsd', 'AQD.xsd') 
    outFile.write(line)

inFile.close()
outFile.close()
相关问题