2017-05-20 1 views
0

Ich habe eine XSD-Datei, die einige ungültige XML erfassen soll, die ich geschrieben habe, aber mein Programm isst es gerne auf. Wie kann ich Validierungsfehler richtig ausgeben?XML-Schema wird nicht als ungültig angezeigt

Das 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="[^@][email protected][^\.]+\..+"/> 
    </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> 

Die ungültige XML-Datei:

<?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> 

ich eine XSD-Datei, die ich einige ungültige xml fangen schrieb soll, aber mein Programm ist es zu essen gerne auf . Wie kann ich Validierungsfehler richtig ausgeben?

+0

Wie liest du das XML und wo laden Sie das Schema? – jdweng

Antwort

0

Die XML ist gültig für dieses Schema. Es ist nicht im selben Namensraum, daher hat das Schema keine Meinung über seine Gültigkeit. Wenn Sie Warnungen aktivieren sind, würden Sie Nachrichten wie diese protokolliert sehen:

  • kann nicht Schemainformationen für das Element ‚Gehäuse‘ gefunden.
  • Die Schemainformationen für das Element 'Listings' konnten nicht gefunden werden.

Ihr Schema hat eine targetNamespace von http://something.com/xmlcourse. Ändern Sie Ihre XML in diesem Namensraum zu sein, und Sie werden diese Fehler sehen:

Siehe this fiddle für eine Demo.