2013-04-24 88 views
1

我有以下XML內容:爲什麼XML驗證失敗?

<?xml version="1.0" encoding="UTF-8"?> 
<success> 
    <accessedResource>/bioportal/provisional</accessedResource> 
    <accessDate>2013-04-23 20:50:58.139 PDT</accessDate> 
    <data> 
    <classBean> 
     <id>http://purl.bioontology.org/ontology/provisional/9cc8b147-e193-4d23-a3a1-9da34eeeb5a9</id> 
     <fullId>http://purl.bioontology.org/ontology/provisional/9cc8b147-e193-4d23-a3a1-9da34eeeb5a9</fullId> 
     <label>OzbbBugGbIINcdpSY</label> 
     <synonyms> 
     <string>ux0fBN http://www.ggiodpc.com/www.6shpFpANPwYnffbs9P5rsRN67oJWDZuQ.com.php</string> 
     </synonyms> 
     <definitions> 
     <string>ux0fBN http://www.ggiodpc.com/www.6shpFpANPwYnffbs9P5rsRN67oJWDZuQ.com.php</string> 
     </definitions> 
     <relations> 
     <entry> 
      <string>provisionalRelatedNoteId</string> 
      <string>Note_3df7809d-aac8-4cf8-b320-f3eecceacd4e</string> 
     </entry> 
     <entry> 
      <string>provisionalTermStatus</string> 
      <null/> 
     </entry> 
     <entry> 
      <string>provisionalCreated</string> 
      <date>2013-04-09 20:06:23.79 PDT</date> 
     </entry> 
     <entry> 
      <string>provisionalPermanentId</string> 
      <null/> 
     </entry> 
     <entry> 
      <string>provisionalRelatedOntologyIds</string> 
      <list> 
      <int>1057</int> 
      </list> 
     </entry> 
     <entry> 
      <string>provisionalSubmittedBy</string> 
      <int>38382</int> 
     </entry> 
     <entry> 
      <string>provisionalUpdated</string> 
      <null/> 
     </entry> 
     <entry> 
      <string>provisionalSubclassOf</string> 
      <org.openrdf.model.URI> 
      <uriString>http://purl.bioontology.org/ontology/RID/RID0</uriString> 
      <localNameIdx>-1</localNameIdx> 
      </org.openrdf.model.URI> 
     </entry> 
     </relations> 
    </classBean> 
    </data> 
</success> 

我想用下面的XML模式對其進行驗證:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
      targetNamespace="http://bioontology.org/bioportal/classBeanSchema#" 
      xmlns:c="http://bioontology.org/bioportal/classBeanSchema#"> 
    <xs:element name="success"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="c:accessedResource"/> 
       <xs:element ref="c:accessDate"/> 
       <xs:element ref="c:data"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="accessedResource" type="xs:string"/> 
    <xs:element name="accessDate" type="xs:string"/> 

    <xs:element name="data"> 
     <xs:complexType> 
      <xs:all> 
       <xs:element ref="c:list" minOccurs="0" maxOccurs="1"/> 
       <xs:element ref="c:classBean" minOccurs="0" maxOccurs="1"/> 
      </xs:all> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="classBean"> 
     <xs:complexType> 
      <xs:choice minOccurs="0" maxOccurs="unbounded"> 
       <xs:element ref="c:fullId" maxOccurs="1" minOccurs="0"/> 
       <xs:element ref="c:id" maxOccurs="1" minOccurs="0"/> 
       <xs:element ref="c:label" maxOccurs="1" minOccurs="0"/> 
       <xs:element ref="c:relations" maxOccurs="1" minOccurs="0"/> 
       <xs:element name="type" type="xs:string" maxOccurs="1" minOccurs="0"/> 
      </xs:choice> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="fullId" type="xs:string"/> 
    <xs:element name="id" type="xs:string"/> 
    <xs:element name="label" type="xs:string"/> 

    <xs:element name="relations"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element minOccurs="0" maxOccurs="unbounded" ref="c:entry"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="entry"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:choice minOccurs="0" maxOccurs="unbounded"> 
        <xs:element name="string" type="xs:string"/> 
        <xs:element ref="c:list"/> 
       </xs:choice> 
       <xs:element minOccurs="0" ref="c:int"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="list"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:choice minOccurs="0" maxOccurs="unbounded"> 
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="c:classBean"/> 
        <xs:element minOccurs="0" maxOccurs="unbounded" name="string" type="xs:string"/> 
       </xs:choice> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="int" type="xs:integer"/> 

</xs:schema> 

因爲我使用下面的代碼片段驗證:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
Schema schema = schemaFactory.newSchema(schemaFile); 
Validator validator = schema.newValidator(); 
validator.validate(xmlFile); 

引發以下異常,這表明XML文件在Schema下無效:

org.xml.sax.SAXParseException; systemId: file:/C:/xml.xml; lineNumber: 2; columnNumber: 10; cvc-elt.1: Cannot find the declaration of element 'success'. 

我不明白這個消息,因爲在第2行中我可以找到'成功'元素。 有人可以幫忙嗎?

+0

看起來像'success'應該在模式中輸入的所有元素。 – leppie 2013-04-25 04:55:15

回答

0

你的XML是錯誤的按照您的架構,看看:

XML:

<classBean> 
    <id>http://purl.bioontology.org/ontology/provisional/9cc8b147-e193-4d23-a3a1-9da34eeeb5a9</id> 
    <fullId>http://purl.bioontology.org/ontology/provisional/9cc8b147-e193-4d23-a3a1-9da34eeeb5a9</fullId> 
    <label>OzbbBugGbIINcdpSY</label> 
    <synonyms> 
    <string>ux0fBN http://www.ggiodpc.com/www.6shpFpANPwYnffbs9P5rsRN67oJWDZuQ.com.php</string> 
    </synonyms> 
    <definitions> 
    <string>ux0fBN http://www.ggiodpc.com/www.6shpFpANPwYnffbs9P5rsRN67oJWDZuQ.com.php</string> 
    </definitions> 

XSD

<xs:element name="classBean"> 
    <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element ref="c:fullId" maxOccurs="1" minOccurs="0"/> 
     <xs:element ref="c:id" maxOccurs="1" minOccurs="0"/> 
     <xs:element ref="c:label" maxOccurs="1" minOccurs="0"/> 
     <xs:element ref="c:relations" maxOccurs="1" minOccurs="0"/> 
     <xs:element name="type" type="xs:string" maxOccurs="1" minOccurs="0"/> 
     </xs:choice> 
    </xs:complexType> 
    </xs:element> 

注: '同義詞' 和「定義'不是'classBean'元素的有效孩子。

'entry'元素與'child'和'null'一樣,但是根據模式,它不是valide。還有一些錯誤。

SAX Parser可能不會反映錯誤的正確行數。