Python 如何使用lxml从XML检索xsi:noNamespaceSchemaLocation?

Python 如何使用lxml从XML检索xsi:noNamespaceSchemaLocation?,python,xml-parsing,lxml,Python,Xml Parsing,Lxml,我正在尝试基于xsi:noNamespaceSchemaLocation验证XML 我研究了这个问题,但似乎没有任何可行的解决方案 我的XML文件如下所示: <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>John Smith</

我正在尝试基于
xsi:noNamespaceSchemaLocation
验证XML

我研究了这个问题,但似乎没有任何可行的解决方案

我的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>

约翰·史密斯
奥拉诺德曼

这是我从根解析和获取attrib时得到的结果
{'{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation':'shiporder.xsd'}


如何使用Python中的
lxml
实现这一点?我查看了其他解析器,但到目前为止还不知道如何做。

感谢@mzjn指出了克拉克符号

我提出的解决方案是:

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

在XML文档中,
xsi
前缀与
http://www.w3.org/2001/XMLSchema-instance
名称空间URI。前缀本身并不标识名称空间(前缀只是一个任意的缩写)。属性的全名为
{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation
(语法称为Clark表示法),这就是您得到的结果。