You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python ElementTree获取并更新XML的xlink:href属性

问题原因

你遇到的KeyError: 'xlink:href'是因为xlink是独立的XML命名空间,ElementTree不会直接将xlink:href作为属性键存储,而是使用完整的命名空间URI拼接属性名:{http://www.w3.org/1999/xlink}href。

可行解决方案

步骤1:定义所有需要的命名空间

除了WMS的命名空间,还要加上xlink的命名空间,方便后续节点定位:

import xml.etree.ElementTree as ET

xmldata='''<?xml version="1.0" encoding="UTF-8"?>
<WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wms http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd">
    <!-- Service Metadata -->
    <Capability>
        <Request>
            <GetCapabilities>
                <Format>text/xml</Format>
                <DCPType>
                    <HTTP>
                        <Get>
                            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
                            xlink:type="simple"
                            xlink:href="http://localhost:8080" />
                        </Get>
                        <Post>
                            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
                            xlink:type="simple"
                            xlink:href="http://localhost:8080" />
                        </Post>
                    </HTTP>
                </DCPType>
            </GetCapabilities>
            <GetMap>
                <Format>image/jpg</Format>
                <Format>image/png</Format>
                <DCPType>
                    <HTTP>
                        <Get>
                            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" 
                            xlink:type="simple" 
                            xlink:href="http://localhost:8080" />
                        </Get>
                    </HTTP>
                </DCPType>
            </GetMap>
            <GetFeatureInfo>
                <Format>text/xml</Format>
                <Format>text/plain</Format>
                <Format>text/html</Format>
            </GetFeatureInfo>
        </Request>
        <Exception>
            <Format>XML</Format>
            <Format>INIMAGE</Format>
            <Format>BLANK</Format>
        </Exception>
    </Capability>
</WMS_Capabilities>'''

# 定义命名空间映射
ns = {
    'wms': 'http://www.opengis.net/wms',
    'xlink': 'http://www.w3.org/1999/xlink'
}
myroot = ET.fromstring(xmldata)

步骤2:正确访问xlink:href属性

有两种直观的访问方式:

方式一:直接用完整命名空间键访问属性
# 定位到GetMap下的OnlineResource节点
online_resource = myroot.find('.//wms:GetMap/wms:DCPType/wms:HTTP/wms:Get/wms:OnlineResource', ns)
# 访问xlink:href属性
current_href = online_resource.attrib['{http://www.w3.org/1999/xlink}href']
print(current_href)  # 输出:http://localhost:8080
方式二:通过属性选择器定位节点
# 直接筛选带xlink:href属性的OnlineResource节点
online_resource = myroot.find('.//wms:OnlineResource[@xlink:href]', ns)
current_href = online_resource.attrib['{http://www.w3.org/1999/xlink}href']
print(current_href)

步骤3:更新属性值并保存XML

# 更新xlink:href属性值
online_resource.attrib['{http://www.w3.org/1999/xlink}href'] = 'http://new-server:9090/wms'

# 保存到XML文件,同时保留XML声明和UTF-8编码
tree = ET.ElementTree(myroot)
with open('updated_wms_capabilities.xml', 'wb') as f:
    tree.write(f, encoding='UTF-8', xml_declaration=True)

额外批量更新提示

如果需要修改所有OnlineResource节点的xlink:href,可以用findall遍历:

for resource in myroot.findall('.//wms:OnlineResource', ns):
    resource.attrib['{http://www.w3.org/1999/xlink}href'] = 'http://new-server:9090/wms'

内容的提问来源于stack exchange,提问作者Kishor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 05:40:52