I have an XSD file that is supposed to catch some invalid xml I wrote, but my program is gladly eating it up. How can I get it to throw validation errors properly?
The schema:
<?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>
The invalid xml file:
<?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>[email protected]</contact_email>
<rent>445.5</rent>
</listing>
</listings>
<last_updated date="2017-05-15" time="12:12:12" />
</housing>
I have an XSD file that is supposed to catch some invalid xml I wrote, but my program is gladly eating it up. How can I get it to throw validation errors properly?
The XML is valid per that schema. It's not in the same namespace, so the schema doesn't actually have any opinion on its validity. If you were to enable warnings, you'd see messages like these logged:
Your schema has a
targetNamespace
ofhttp://something.com/xmlcourse
. Change your XML to be in this namespace and you'll see these errors:See this fiddle for a demo.