在python minidom中更改名称空间

2024-05-14 00:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下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:模式定位="http://www.exampleURI.com/AQDaqd/1.0标准/AQD.xsd标准'发生,我只想更改'aqd/1.0/AQD.xsd标准'到AQD.xsd标准在

谢谢你的帮助


Tags: orgopengishttpnetwwwamxsdinspire
1条回答
网友
1楼 · 发布于 2024-05-14 00:38:53

速度慢,不需要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()

相关问题 更多 >