lxml如何在同时加载内联架构的同时针对XSD文件验证某些XML? [英] How can lxml validate some XML against both an XSD file while also loading an inline schema too?

查看:51
本文介绍了lxml如何在同时加载内联架构的同时针对XSD文件验证某些XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取lxml来成功验证某些xml时遇到问题. XSD模式和XML文件均来自Amazon文档,因此应该兼容.但是XML本身引用的是另一个未加载的模式.

I'm having problems getting lxml to successfully validate some xml. The XSD schema and XML file are both from Amazon documentation so should be compatible. But the XML itself refers to another schema that's not being loaded.

这是我的代码,它基于 lxml验证教程:

Here is my code, which is based on the lxml validation tutorial:

xsd_doc = etree.parse('ProductImage.xsd')
xsd = etree.XMLSchema(xsd_doc)
xml = etree.parse('ProductImage_sample.xml')
xsd.validate(xml)
print xsd.error_log

"ProductImage_sample.xml:2:0:ERROR:SCHEMASV:SCHEMAV_CVC_ELT_1:元素'AmazonEnvelope':没有匹配的全局声明可用于验证根."

"ProductImage_sample.xml:2:0:ERROR:SCHEMASV:SCHEMAV_CVC_ELT_1: Element 'AmazonEnvelope': No matching global declaration available for the validation root."

如果我针对amzn-envelope.xsd而不是ProductImage.xsd进行验证,则不会出现任何错误,但这使查看给定的图像提要是否有效这一点失去了意义.所有xsd&提到的xml文件和我的python脚本都在我的工作目录中.

I get no errors if I validate against amzn-envelope.xsd instead of ProductImage.xsd, but that defeats the point of seeing if a given Image feed is valid. All xsd & xml files mentioned are in my working directory along with my python script by the way.

这是示例xml的摘要,该片段肯定是有效的:

Here is a snippet of the sample xml, which should definately be valid:

<?xml version="1.0"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
    <Header>
        <DocumentVersion>1.01</DocumentVersion>
        <MerchantIdentifier>Q_M_STORE_123</MerchantIdentifier>
    </Header>
    <MessageType>ProductImage</MessageType>
    <Message>
        <MessageID>1</MessageID>
        <OperationType>Update</OperationType>
        <ProductImage>
            <SKU>1234</SKU>

这是模式的摘要(此文件不是公共文件,因此我无法全部显示):

Here is a snippet of the schema (this file is not public so I can't show all of it):

<?xml version="1.0"?>
<!-- Revision="$Revision: #5 $" -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xsd:include schemaLocation="amzn-base.xsd"/>
    <xsd:element name="ProductImage">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="SKU"/>

我可以说,在包含到amzn-base.xsd之后并没有最终达到AmazonEnvelope标记的定义.所以我的问题是:lxml是否可以通过<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">之类的标签加载架构.如果没有,该如何验证我的图片供稿?

I can say that following the include to amzn-base.xsd does not end up reaching a definition of the AmazonEnvelope tag. So my questions is: can lxml load schemas via a tag like <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">. And if not, how can I validate my Image feed?

推荐答案

答案是我应该由父模式文件,这在XML文件的顶部如上所述验证是AMZN-envelope.xsd因为这包含了线:

The answer is I should validate by the parent schema file, which as mentioned at the top of the XML file is amzn-envelope.xsd as this contains the line:

<xsd:include schemaLocation="ProductImage.xsd"/>

在一般的话,LXML不会阅读这种声明的XSI:no​​NamespaceSchemaLocation =AMZN-envelope.xsd",但如果你能找到父模式来验证那么这应该希望包括您的具体模式有兴趣内.

In general then, lxml won't read such a declaration as xsi:noNamespaceSchemaLocation="amzn-envelope.xsd" but if you can find the parent schema to validate against then this should hopefully include the specific schema you're interested in.

这篇关于lxml如何在同时加载内联架构的同时针对XSD文件验证某些XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆