2017-05-20 7 views
0

私が書いた無効なxmlをキャッチするはずの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="[^@][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> 

無効な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>[email protected]</contact_email> 
     <rent>445.5</rent> 
    </listing> 
    </listings> 
    <last_updated date="2017-05-15" time="12:12:12" /> 
</housing> 

私は私が書いたいくつかの無効なXMLをキャッチすることになっているXSDファイルを持っていますが、私のプログラムは喜んでそれを食べています。検証エラーを正しくスローするにはどうすればよいですか?

+0

xmlをどのように読んでいますか?スキーマはどこにロードされていますか? – jdweng

答えて

0

XML は、そのスキーマで有効なです。それは同じ名前空間にはないので、スキーマは実際にその正当性について意見を持っていません。あなたが警告を有効にした場合は、これらのログに記録のようなメッセージを参照してくださいね。

  • は、要素の住宅 'のスキーマ情報を見つけることができませんでした。
  • 要素 'listings'のスキーマ情報が見つかりませんでした。

あなたのスキーマはhttp://something.com/xmlcoursetargetNamespaceています。この名前空間にあるように、あなたのXMLを変更して、あなたはこれらのエラーが表示されます:

デモはthis fiddleを参照してください。

関連する問題