从XML标签获取URL

问题描述 投票:1回答:2

我的XML文件:

<xml 
xmlns="http://www.myweb.org/2003/instance"
xmlns:link="http://www.myweb.org/2003/linkbase"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:iso4217="http://www.myweb.org/2003/iso4217"
xmlns:utr="http://www.myweb.org/2009/utr">

<link:schemaRef xlink:type="simple" xlink:href="http://www.myweb.com/form/2020-01-01/test.xsd"></link:schemaRef>

我想从http://www.myweb.com/folder/form/1/2020-01-01/test.xsd标签获得URL:<link:schemaRef>

我下面的python代码找到<link:schemaRef>标记。但是我无法检索该URL。

from lxml import etree
with open(filepath,'rb') as f:
     file = f.read()    
root = etree.XML(file)
print(root.nsmap["link"]) #http://www.myweb.org/2003/linkbase
print(root.find(".//{"+root.nsmap["link"]+"}"+"schemaRef")) 
python lxml
2个回答
0
投票

用途:


0
投票

以这种方式尝试,看看是否有效:

© www.soinside.com 2019 - 2024. All rights reserved.