我在OS X上使用xmllint尝试针对XSD验证XML文件。
示例XML是:
<?xml version="1.0" encoding="UTF-8"?>
<volume_entries>
<volume_entry>
<period>2011-08-01</period>
<volume units="lb">15.23</volume>
<uuid>64091d84-2687-11dd-a4e9-daf105bfb8c2</uuid>
<serial_ident>31515-1251512</serial_ident>
<supplier>Supplier A</supplier>
<category type="wild_farmed">Wild farmed A</category>
<category type="procurement">Procurement option A</category>
<category type="latin_name">latin_name A</category>
<category type="wild_farmed">WILD CAUGHT</category>
</volume_entry>
</volume_entries>
示例XSD是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="volume_entries">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="volume_entry"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="volume_entry">
<xs:complexType>
<xs:sequence>
<xs:element ref="period"/>
<xs:element ref="volume"/>
<xs:element ref="uuid"/>
<xs:element ref="serial_ident"/>
<xs:element ref="supplier"/>
<xs:element ref="category"/>
<xs:element ref="category"/>
<xs:element ref="category"/>
<xs:element ref="category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="period" type="xs:date"/>
<xs:element name="volume">
<xs:annotation>
<xs:documentation>Volumes are decimals with a string 'unit' attribute that can be one of: kg - kilograms, lb - pounds, t - metric tonne, LT - long ton (UK), ST - short ton (US).</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="units" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="kg"/>
<xs:enumeration value="lb"/>
<xs:enumeration value="t"/>
<xs:enumeration value="LT"/>
<xs:enumeration value="ST"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="uuid" type="xs:string"/>
<xs:element name="serial_ident" type="xs:string"/>
<xs:element name="supplier" type="xs:string"/>
<xs:element name="category">
<xs:annotation>
<xs:documentation>Category is a string with a string 'type' attribute that can be one of 'procurement', 'latin_name', 'harvest_country' or 'wild_farmed'</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="procurement"/>
<xs:enumeration value="latin_name"/>
<xs:enumeration value="harvest_country"/>
<xs:enumeration value="wild_farmed"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
如果我尝试通过解析整个文档来验证,例如:
$ xmllint --schema /Users/mezza9/sample.xsd /Users/mezza9/sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<volume_entries>
<volume_entry>
<period>2011-08-01</period>
<volume units="lb">15.23</volume>
<uuid>64091d84-2687-11dd-a4e9-daf105bfb8c2</uuid>
<serial_ident>31515-1251512</serial_ident>
<supplier>Supplier A</supplier>
<category type="wild_farmed">Wild farmed A</category>
<category type="procurement">Procurement option A</category>
<category type="latin_name">latin_name A</category>
<category type="wild_farmed">WILD CAUGHT</category>
</volume_entry>
</volume_entries>
/Users/mezza9/sample.xml validates
然后文件验证。
但是,如果我使用流媒体API,验证失败了吗?!
$ xmllint --stream --schema /Users/mezza9/sample.xsd /Users/mezza9/sample.xml
/Users/mezza9/sample.xml:5: Schemas validity error : Element 'volume', attribute 'units': [facet 'enumeration'] The value '' is not an element of the set {'kg', 'lb', 't', 'LT', 'ST'}.
/Users/mezza9/sample.xml:5: Schemas validity error : Element 'volume', attribute 'units': '' is not a valid value of the local atomic type.
/Users/mezza9/sample.xml:9: Schemas validity error : Element 'category', attribute 'type': [facet 'enumeration'] The value '' is not an element of the set {'procurement', 'latin_name', 'harvest_country', 'wild_farmed'}.
/Users/mezza9/sample.xml:9: Schemas validity error : Element 'category', attribute 'type': '' is not a valid value of the local atomic type.
/Users/mezza9/sample.xml:10: Schemas validity error : Element 'category', attribute 'type': [facet 'enumeration'] The value '' is not an element of the set {'procurement', 'latin_name', 'harvest_country', 'wild_farmed'}.
/Users/mezza9/sample.xml:10: Schemas validity error : Element 'category', attribute 'type': '' is not a valid value of the local atomic type.
/Users/mezza9/sample.xml:11: Schemas validity error : Element 'category', attribute 'type': [facet 'enumeration'] The value '' is not an element of the set {'procurement', 'latin_name', 'harvest_country', 'wild_farmed'}.
/Users/mezza9/sample.xml:11: Schemas validity error : Element 'category', attribute 'type': '' is not a valid value of the local atomic type.
/Users/mezza9/sample.xml:12: Schemas validity error : Element 'category', attribute 'type': [facet 'enumeration'] The value '' is not an element of the set {'procurement', 'latin_name', 'harvest_country', 'wild_farmed'}.
/Users/mezza9/sample.xml:12: Schemas validity error : Element 'category', attribute 'type': '' is not a valid value of the local atomic type.
/Users/mezza9/sample.xml fails to validate
我正在使用xmllint的当前系统版本:
$ xmllint --version
xmllint: using libxml version 20904
compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude ICU ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib
$ which xmllint
/usr/bin/xmllint
非常感谢任何建议。