为什么XML验证失败?

时间:2013-04-24 04:12:16

标签: xml validation schema saxparseexception

我有以下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 Schema验证它:

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

抛出以下异常,这表明在给定Schema的情况下XML文件无效:

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

我不明白这条消息,因为在第2行我可以找到'success'元素。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

根据您的Schema,您的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>

注意:'synonyms'和'definitions'不是'classBean'元素的有效子。

同样的事情对于'entry'元素,其中'date'和'null'作为子元素但是根据模式它不是valide。还有一些错误。

SAX Parser可能无法反映错误的错误行数。