首页 > 解决方案 > 发现以元素“描述”开头的无效内容。'{"http://test.com/test/v1.0":description}' 之一是预期的

问题描述

爪哇代码:

   public static boolean validateXMLSchema(){
      try {
         SchemaFactory factory =
            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File("students.xsd"));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File("students.xml")));
      } catch (IOException e){
         System.out.println("Exception: "+e.getMessage());
         return false;
      }catch(SAXException e1){
         System.out.println("SAX Exception: "+e1.getMessage());
         return false;
      }

学生.xsd 文件

<?xml version = "1.0"?>
<xs:schema elementFormDefault="qualified"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:midi="http://test.com/test/v1.0">

   <xs:import namespace="http://test.com/test/v1.0" schemaLocation="midi.xsd"/>

   <xs:element name = 'class'>
      <xs:complexType>
         <xs:sequence>
             <xs:element name = 'student' type = 'StudentType' minOccurs = '1' 
                maxOccurs = '1' />
         <xs:element name='rule' type='midi:rule' minOccurs = '1' 
                maxOccurs = '1' />
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:complexType name = "StudentType">
      <xs:sequence>
         <xs:element name = "firstname" type = "xs:string"/>
         <xs:element name = "marks" type = "xs:positiveInteger"/>
      </xs:sequence>
      <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
   </xs:complexType>             
</xs:schema>

midi.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://test.com/test/v1.0"
    elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://test.com/test/v1.0">

     <xs:complexType name="rule">
          <xs:sequence>
            <xs:element name="description" type="xs:string" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
     </xs:complexType>
<!--    <xs:element name="rule" type="rule"/>-->

</xs:schema>

和我的 xml 文件

<?xml version = "1.0"?>
<class>  
   <student rollno = "593">    
      <firstname>Jasvir</firstname>
      <marks>90</marks>
   </student>
  <rule >
   <description>test</description>
  </rule>
</class>

当我执行 java 代码时,任何人都可以帮助我,然后我得到以下错误 SAX 异常:cvc-complex-type.2.4.a:发现以元素“描述”开头的无效内容。需要 '{" http://test.com/test/v1.0 ":description}' 之一。无效反对

标签: java

解决方案


推荐阅读