XML 架构未显示为无效
XML Schema not Showing as Invalid
我有一个 XSD 文件,它应该捕获我写的一些无效 xml,但我的程序很乐意吃掉它。我怎样才能让它正确抛出验证错误?
架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://something.com/xmlcourse"
elementFormDefault="qualified"
xmlns="http://something.com/xmlcourse"
xmlns:lexc="http://something.com/xmlcourse"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<!-- Type Definitions -->
<xs:simpleType name="nonNegativeDecimal">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="emailAddress">
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^\.]+\..+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="phoneNumber">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{3}[-\.][0-9]{3}[-\.][0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="address">
<xs:restriction base="xs:string">
<xs:enumeration value="apartment"/>
<xs:enumeration value="condominium"/>
<xs:enumeration value="townhouse"/>
<xs:enumeration value="duplex"/>
<xs:enumeration value="house"/>
<xs:enumeration value="residence"/>
</xs:restriction>
</xs:simpleType>
<!-- Attributes are used for date and time on last_updated -->
<xs:complexType name="dateAndTime">
<xs:attribute name="date" type="xs:date"></xs:attribute>
<xs:attribute name="time" type="xs:time"></xs:attribute>
</xs:complexType>
<xs:complexType name="listing">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="type" type="lexc:address" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="contact_name" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="contact_phone" type="lexc:phoneNumber" minOccurs="1" maxOccurs="3"></xs:element>
<xs:element name="contact_email" type="lexc:emailAddress" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="rent" type="lexc:nonNegativeDecimal" minOccurs="1" maxOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
<!-- Schema Instance Contents -->
<xs:element name="housing">
<xs:complexType>
<xs:sequence>
<xs:element name="listings">
<xs:complexType>
<xs:sequence>
<xs:element name="listing" type="lexc:listing" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueId">
<xs:selector xpath="lexc:listing"></xs:selector>
<xs:field xpath="@id"></xs:field>
</xs:unique>
</xs:element>
<xs:element name="last_updated" type="lexc:dateAndTime" minOccurs="1" maxOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
无效的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<housing>
<listings>
<listing>
<id>apt1</id>
<type>wrongtype</type> <!-- Type does not exist -->
<address>556 Huckaby Lane</address>
<contact_name>John Spacer</contact_name>
<contact_phone>555-555-5555</contact_phone> <!-- Over Max of 3 Phone Numbers-->
<contact_phone>555-555-5555</contact_phone>
<contact_phone>555-555-5555</contact_phone>
<contact_phone>555-555-5555</contact_phone>
<contact_email>kestrel.gmail.com</contact_email> <!-- Wrong Format for Email Address -->
<rent>123.45</rent>
</listing>
<listing>
<id>cdm1</id>
<type>condominium</type>
<address>123 Saskatoon Lane</address>
<contact_name>George Romero</contact_name>
<contact_phone>123.456.7890</contact_phone>
<contact_email>jasper@montypython.com</contact_email>
<rent>445.5</rent>
</listing>
</listings>
<last_updated date="2017-05-15" time="12:12:12" />
</housing>
我有一个 XSD 文件,它应该捕获我写的一些无效 xml,但我的程序很乐意吃掉它。我怎样才能让它正确抛出验证错误?
XML 根据该架构有效。它不在同一个命名空间中,因此模式实际上对其有效性没有任何意见。如果您要启用警告,您会看到如下记录的消息:
- Could not find schema information for the element 'housing'.
- Could not find schema information for the element 'listings'.
您的架构的 targetNamespace
为 http://something.com/xmlcourse
。将您的 XML 更改为在此命名空间中,您将看到以下错误:
- The 'http://something.com/xmlcourse:type' element is invalid - The value 'wrongtype' is invalid according to its datatype 'http://something.com/xmlcourse:address' - The Enumeration constraint failed.
- The element 'listing' in namespace 'http://something.com/xmlcourse' has invalid child element 'contact_phone' in namespace 'http://something.com/xmlcourse'. List of possible elements expected: 'contact_email, rent' in namespace 'http://something.com/xmlcourse'.
有关演示,请参阅 this fiddle。
我有一个 XSD 文件,它应该捕获我写的一些无效 xml,但我的程序很乐意吃掉它。我怎样才能让它正确抛出验证错误?
架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://something.com/xmlcourse"
elementFormDefault="qualified"
xmlns="http://something.com/xmlcourse"
xmlns:lexc="http://something.com/xmlcourse"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<!-- Type Definitions -->
<xs:simpleType name="nonNegativeDecimal">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="emailAddress">
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^\.]+\..+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="phoneNumber">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{3}[-\.][0-9]{3}[-\.][0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="address">
<xs:restriction base="xs:string">
<xs:enumeration value="apartment"/>
<xs:enumeration value="condominium"/>
<xs:enumeration value="townhouse"/>
<xs:enumeration value="duplex"/>
<xs:enumeration value="house"/>
<xs:enumeration value="residence"/>
</xs:restriction>
</xs:simpleType>
<!-- Attributes are used for date and time on last_updated -->
<xs:complexType name="dateAndTime">
<xs:attribute name="date" type="xs:date"></xs:attribute>
<xs:attribute name="time" type="xs:time"></xs:attribute>
</xs:complexType>
<xs:complexType name="listing">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="type" type="lexc:address" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="contact_name" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="contact_phone" type="lexc:phoneNumber" minOccurs="1" maxOccurs="3"></xs:element>
<xs:element name="contact_email" type="lexc:emailAddress" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="rent" type="lexc:nonNegativeDecimal" minOccurs="1" maxOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
<!-- Schema Instance Contents -->
<xs:element name="housing">
<xs:complexType>
<xs:sequence>
<xs:element name="listings">
<xs:complexType>
<xs:sequence>
<xs:element name="listing" type="lexc:listing" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueId">
<xs:selector xpath="lexc:listing"></xs:selector>
<xs:field xpath="@id"></xs:field>
</xs:unique>
</xs:element>
<xs:element name="last_updated" type="lexc:dateAndTime" minOccurs="1" maxOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
无效的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<housing>
<listings>
<listing>
<id>apt1</id>
<type>wrongtype</type> <!-- Type does not exist -->
<address>556 Huckaby Lane</address>
<contact_name>John Spacer</contact_name>
<contact_phone>555-555-5555</contact_phone> <!-- Over Max of 3 Phone Numbers-->
<contact_phone>555-555-5555</contact_phone>
<contact_phone>555-555-5555</contact_phone>
<contact_phone>555-555-5555</contact_phone>
<contact_email>kestrel.gmail.com</contact_email> <!-- Wrong Format for Email Address -->
<rent>123.45</rent>
</listing>
<listing>
<id>cdm1</id>
<type>condominium</type>
<address>123 Saskatoon Lane</address>
<contact_name>George Romero</contact_name>
<contact_phone>123.456.7890</contact_phone>
<contact_email>jasper@montypython.com</contact_email>
<rent>445.5</rent>
</listing>
</listings>
<last_updated date="2017-05-15" time="12:12:12" />
</housing>
我有一个 XSD 文件,它应该捕获我写的一些无效 xml,但我的程序很乐意吃掉它。我怎样才能让它正确抛出验证错误?
XML 根据该架构有效。它不在同一个命名空间中,因此模式实际上对其有效性没有任何意见。如果您要启用警告,您会看到如下记录的消息:
- Could not find schema information for the element 'housing'.
- Could not find schema information for the element 'listings'.
您的架构的 targetNamespace
为 http://something.com/xmlcourse
。将您的 XML 更改为在此命名空间中,您将看到以下错误:
- The 'http://something.com/xmlcourse:type' element is invalid - The value 'wrongtype' is invalid according to its datatype 'http://something.com/xmlcourse:address' - The Enumeration constraint failed.
- The element 'listing' in namespace 'http://something.com/xmlcourse' has invalid child element 'contact_phone' in namespace 'http://something.com/xmlcourse'. List of possible elements expected: 'contact_email, rent' in namespace 'http://something.com/xmlcourse'.
有关演示,请参阅 this fiddle。