简体   繁体   English

无法针对xsd验证xml

[英]unable to validate xml against xsd

I am new to learning XSD . 我是学习XSD新手。 I wrote a XSD file and and XML file and a program to validate the XML against XSD. 我编写了一个XSD文件和XML文件以及一个针对XSD验证XML的程序。 When I run the program I am getting the error stating Invalid content was found starting with element 'id'. One of '{id}' is expected 当我运行程序时,我收到错误,指出Invalid content was found starting with element 'id'. One of '{id}' is expected Invalid content was found starting with element 'id'. One of '{id}' is expected . Invalid content was found starting with element 'id'. One of '{id}' is expected Can someone please explain where I am going wrong. 有人可以解释我哪里出错了。

XSD file: XSD文件:

<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.edureka.co/Student" 
                xmlns:st="http://www.edureka.co/Student"
                >
        <xs:element name="student"  type="st:student"></xs:element>
            <xs:complexType name="student">
                <xs:sequence>
                    <xs:element name="id" type="xs:int"/>
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="language" type="xs:string"/>
                    <xs:element name="expertise" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>

And the XML file: 和XML文件:

<?xml version="1.0" encoding="UTF-8"?>
    <student xmlns="http://www.edureka.co/Student"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.edureka.co/Student studentRule.xsd"
             >

       <id>1234</id>
       <name>Pradeep</name>
       <language>Sanskrit</language>
       <expertise>Beginner</expertise>
    </student>

The Java program to validate the XML file: 用于验证XML文件的Java程序:

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class ValidatorDemo {

    public static void main(String[] args) {

      System.out.println("student.xml validates against studentRule.xsd? "+validateXMLSchema("studentRule.xsd", "student.xml"));

      }

    public static boolean validateXMLSchema(String xsdPath, String xmlPath){

        try {
            SchemaFactory factory = 
                    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
        } catch (IOException | SAXException e) {
            System.out.println("Exception: "+e.getMessage());
            return false;
        }
        return true;
    }
}

Add the attribute elementFormDefault="qualified" to the root element of your schema. 将属性elementFormDefault="qualified"到架构的根元素。

The default is "unqualified", which can't be used when specifying a default namespace. 默认值为“unqualified”,在指定默认命名空间时不能使用。

Alternatively, you could leave your schema as is, and update your document to use the unqualified format (no default namespace, and only qualify the global elements): 或者,您可以保留模式,并更新文档以使用非限定格式(没有默认名称空间,只限定全局元素):

<st:student xmlns:st="http://www.edureka.co/Student"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.edureka.co/Student studentRule.xsd"
        >

    <id>1234</id>
    <name>Pradeep</name>
    <language>Sanskrit</language>
    <expertise>Beginner</expertise>
</st:student>

The xml document you posted is in "qualified" format; 您发布的xml文档采用“合格”格式; every element is explicitly associated with a namespace, which you did by specifying a default - xmlns="http://www.edureka.co/Student" . 每个元素都与命名空间显式关联,您可以通过指定默认值来实现 - xmlns="http://www.edureka.co/Student"

The xml above is in "unqualified" format; 上面的xml是“不合格”的格式; the global elements (ie the top level elements in the schema) have to be qualified, but the local elements (ie the nested elements in the schema) must be unqualified. 全局元素(即模式中的顶级元素)必须是限定的,但本地元素(即模式中的嵌套元素) 必须是不合格的。 In "unqualified" format the local elements are implicitly associated with the namespace of the enclosing global element. 在“非限定”格式中,本地元素与封闭全局元素的命名空间隐式关联。

A default namespace can't be used in documents that use "unqualified" because it explicitly associates elements without a namespace prefix with a namespace. 默认名称空间不能在使用“unqualified”的文档中使用,因为它显式地将没有名称空间前缀的元素与名称空间相关联。 Think of it as associating "the empty prefix" with a namespace. 可以将其视为将“空前缀”与命名空间相关联。

In general, the "qualified" format is the easiest one to use. 通常,“合格”格式是最容易使用的格式。

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

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