[英]Who is right and who does the faulty validation (Me, XMLSpy, lxml.etree or xmllint)
我嘗試使用后面的xsd文件在同一文件夾中驗證以下XML。 根據Altova XMLSpy的說法,這是完全有效的,但是為了幫助一些沒有許可證的同事找到我嘗試用python和'lxml.etree'以及xmllint驗證文件的基本錯誤。 這兩個說xml對同一個消息無效:
machineDB.xml:20:模式有效性錯誤:元素'canframe':找不到keyref'busRef'的key-sequence ['remotebus']匹配項。 machineDB.xml無法驗證
有人可以幫忙找到任何人的錯嗎?
版本:
Altova XMLSpy專業版2016版rel。 2 sp1(x64)
lxml.etree版本
Python:sys.version_info(major = 2,minor = 7,micro = 11,releaselevel ='final',serial = 0)lxml.etree:(3,7,2,0)libxml使用:(2,9,4) )libxml編譯:(2,9,4)libxslt使用:(1,1,29)libxslt編譯:(1,1,29)
xmllint(使用libxml版本20708)
machineDB.xml文件:
<machinedb xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="machinedb.xsd">
<busdefinition>
<bus name="displaybus"></bus>
<bus name="remotebus"></bus>
</busdefinition>
<cdefinition>
<c>
<canbus bus_ref="remotebus"></canbus>
<canbus bus_ref="displaybus"></canbus>
</c>
<c>
<canbus bus_ref="displaybus"></canbus>
</c>
<c>
<canbus bus_ref="remotebus"></canbus>
</c>
</cdefinition>
<sdefinition>
<s>
<canframe bus_ref="remotebus"></canframe>
</s>
</sdefinition>
</machinedb>
machinedb.xsd文件:
<xs:schema xmlns:altova="http://www.altova.com/xml-schema-extensions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="machinedb">
<xs:complexType>
<xs:sequence>
<xs:element name="busdefinition" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="bus" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="NameType" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="cdefinition" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="c" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name="canbus" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="bus_ref" type="NameType" use="required"/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sdefinition" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="s" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name="canframe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="bus_ref" use="required"/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="busKey">
<xs:selector xpath="busdefinition/bus"/>
<xs:field xpath="@name"/>
</xs:key>
<xs:keyref name="busRef" refer="busKey">
<xs:selector xpath="cdefinition/c/canbus |sdefinition/s/canframe"/>
<xs:field xpath="@bus_ref"/>
</xs:keyref>
</xs:element>
<xs:simpleType name="NameType">
<xs:restriction base="xs:string">
<xs:pattern value="[\w_]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
有趣的問題。 問題消失,如果你設置的type
的bus_ref
和canbus
到xs:string
沒有一個自定義的限制(見這個問題 ):
<xs:attribute name="bus_ref" type="xs:string" use="required"/>
<!--...-->
<xs:attribute name="bus_ref" use="required" type="xs:string"/>
我認為 (瘋狂的猜想)這是使用libxml並且Xerces和Saxon行為正確的工具的特定缺點。
架構包含屬性
vc:minVersion="1.1"
這表明它是XSD 1.1架構。 使用Liquid XML驗證它我得到以下結果
使用.Net驗證閱讀器報告它是有效的。 .Net解析器是一個XSD 1.0解析器,並且不知道vc:minVersion屬性,所以只是忽略它, 將其視為1.0模式 。
使用.Xerces在XSD 1.0模式下驗證無法驗證。 Xerces知道vc:minVersion屬性, 因此忽略了模式,因為它不在1.1模式下。
使用.Xerces在XSD 1.1模式下驗證它是否有效 。 Xerces知道vc:minVersion,可以使用XSD 1.1標准進行驗證,並認為一切正常。
其他不支持XSD 1.1的解析器可以采用任何一種方式。
很明顯,我不認為架構包含1.1特定的(語法或功能),所以我不確定為什么它被標記為1.1架構。
但是回到你得到的錯誤,我認為這是你正在使用的解析器的一個怪癖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.