简体   繁体   English

XML模式验证问题

[英]XML Schema Validation Issue

I'm trying to validate an XML file against XSD Schema that I've written before. 我正在尝试根据我之前编写的XSD Schema验证XML文件。 The java code to validate my xml file is shown below. 验证我的xml文件的Java代码如下所示。 When I try to validate the XML, I always get an error like : " Cannot find declaration of the root element ". 当我尝试验证XML时,总是会收到类似“ 找不到根元素的声明 ”的错误。

Could you help me to solve this problem? 您能帮我解决这个问题吗?

XML file XML文件

<?xml version="1.0" encoding="UTF-8"?>
<AllBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://myNameSpace.com"  
        schemaLocation="http://myNameSpace.com book.xsd">
    <book>
        <id>1</id>
        <title>aşk ve gurur</title>
        <author>james brown</author>
        <category>science</category>
        <availablity>100</availablity>
        <price>5000</price>
    </book>
    <book>
        <id>2</id>
        <title>kskkdn</title>
        <author>mşlfke</author>
        <category>love</category>
        <availablity>50</availablity>
        <price>5000</price>
    </book>
</AllBooks>

Schema file 模式文件

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

    <xs:element name="AllBooks">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="id" type="xs:integer"/>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="author" type="xs:string"/>
                            <xs:element name="category" type="xs:string"/>
                            <xs:element name="availability" type="xs:integer"/>
                            <xs:element name="price" type="xs:integer"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

And java code 和java代码

static boolean validateAgainstXSD(InputStream xml, InputStream xsd)
{
    try
    {
        SchemaFactory factory = 
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xml));
        return true;
    }
    catch(Exception ex)
    {
        return false;
    }
}

Your namespaces do not match up between the XSD and XML files. 您的名称空间在XSD和XML文件之间不匹配。 Also, availability is misspelled as availablity in the XML file. 此外, availability拼错为availablity XML文件英寸 Corrections follow... 更正如下...

Use this XSD: 使用此XSD:

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

  <xs:element name="AllBooks">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="id" type="xs:integer"/>
              <xs:element name="title" type="xs:string"/>
              <xs:element name="author" type="xs:string"/>
              <xs:element name="category" type="xs:string"/>
              <xs:element name="availability" type="xs:integer"/>
              <xs:element name="price" type="xs:integer"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Then this fixed XML instance document will be valid: 然后,此固定的XML实例文档将有效:

<?xml version="1.0" encoding="UTF-8"?>
<AllBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://myNameSpace.com"  
             xsi:schemaLocation="http://myNameSpace.com book.xsd">
    <book>
        <id>1</id>
        <title>aşk ve gurur</title>
        <author>james brown</author>
        <category>science</category>
        <availability>100</availability>
        <price>5000</price>
      </book>
    <book>
        <id>2</id>
        <title>kskkdn</title>
        <author>mşlfke</author>
        <category>love</category>
        <availability>50</availability>
        <price>5000</price>
    </book>
</AllBooks>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM