如何使用lxml从XML检索xsi:noNamespaceSchemaLocation? [英] How to retrieve xsi:noNamespaceSchemaLocation from XML with lxml?
问题描述
我正在尝试基于xsi:noNamespaceSchemaLocation
验证XML.
I am trying to validate XML based on xsi:noNamespaceSchemaLocation
.
我研究了这个问题,但似乎没有任何可用的解决方案.
I researched this question but it doesn't seem any available solutions for it.
我的XML文件如下:
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
我从 w3school
这是我从根目录解析并获取attrib时得到的
{'{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation': 'shiporder.xsd'}
This is what I get when parse and take attrib from root
{'{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation': 'shiporder.xsd'}
如何在Python中使用lxml
做到这一点?我查看了其他解析器,但到目前为止还不知道该怎么做.
How can I do it with lxml
in Python? I looked on other parsers but so far no idea how to do it.
推荐答案
感谢@mzjn指出了Clark表示法.
Thanks to @mzjn for pointing out about Clark notation.
我想出的解决方案是:
from lxml import etree
...
it = etree.fromstring(xml)
# We need to go through all keys since they can be in
# Clark notation and have URL with brackets as a prefix
for attr in it.attrib:
if 'noNamespaceSchemaLocation' in attr:
xsd = it.attrib.get(attr)
break
...
# Do validations based on XSD URL value
这篇关于如何使用lxml从XML检索xsi:noNamespaceSchemaLocation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!