2

我有一个 XML 文件和一个 XSD 文件,我想根据 XSD 验证 XML。

但我不断收到以下错误:

org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document '/connector/connector.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

我打印了canonical path以确保我正在尝试使用正确的文件。但它不会工作。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <Content>
<commandLine>
<commandCode>A1</commandCode>
<marks><mark><code>mail</code><value>test@test.com</value></mark></marks>
<customerID>1</customerID>
<MessageType>2</MessageType>
</commandLine>
<Antwoordregel></Antwoordregel>
</Content>
</xs:schema>

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Content">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="commandLine" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence minOccurs="1" maxOccurs="1">
                            <xs:element name="commandCode" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="marks" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:sequence >
                                        <xs:element name="mark" minOccurs="1" maxOccurs="unbounded">
                                            <xs:complexType>
                                                <xs:sequence >                                                             
                                                    <xs:element name="code" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                                    <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>                                                                
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>   
                            <xs:element name="customerID" type="xs:string" minOccurs="0" maxOccurs="1"/>
                            <xs:element name="MessageType" type="xs:string" minOccurs="0" maxOccurs="1"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Antwoordregel" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="1">
                            <xs:element name="resultCode" type="xs:string" minOccurs="0" maxOccurs="1"/>
                            <xs:element name="statusInfo" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="transactionInfo" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

我用来验证的代码:

static boolean validateAgainstXSD(String xml){
        try{
            File xsd = new File("connector/connector.xsd");
            System.out.println(xsd.getCanonicalPath());
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new StreamSource("/connector/connector.xsd"));

            System.out.println(schema.toString());
            Validator validator  = schema.newValidator();
            validator.validate(new StreamSource(xml));
            return true;
        }catch(Exception exe){
            exe.printStackTrace();
        return false;
        }
    }

它总是返回 false。我尝试使用在线工具通过 XSD 验证 XML,该工具可在此处找到:www.utilities-online.info/xsdvalidation。此验证器返回:

Not valid.
Error - Line 2, 122: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'xs:schema'.

我能做些什么来解决这个问题?

任何帮助表示赞赏,

谢谢!

4

3 回答 3

3

从 XML 文件中删除第二行

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified">

正确的 XML 是

<?xml version="1.0" encoding="UTF-8"?>
<Content>
    <commandLine>
        <commandCode>A1</commandCode>
        <marks><mark><code>mail</code><value>test@test.com</value></mark></marks>
        <customerID>1</customerID>
        <MessageType>2</MessageType>
    </commandLine>
    <Antwoordregel></Antwoordregel>
</Content>

解决方案 :

请检查文件夹结构并再次运行。

源代码:

package com.shashi.mpoole;


import java.io.File;

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;

public class XMLValid {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        if(validateAgainstXSD(new File("connector/connector.xml")))
        {
            System.out.println("Success");
        }
        else
        {
            System.out.println("Failure");
        }
    }



    static boolean validateAgainstXSD(File xml){
        try{

            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new StreamSource("connector/connector.xsd"));

            Validator validator  = schema.newValidator();
            validator.validate(new StreamSource(xml));
            return true;
        }catch(Exception exe){
            exe.printStackTrace();
        return false;
        }
    }

}

文件夹结构

-----Project

    ------------- src

                  -------------XMLValid.java

    ------------- connector

                  -------------connector.xsd

                  -------------connector.xml 
于 2013-01-21T11:14:18.230 回答
2

我能够通过更改命名空间前缀为“ xs: ”的 XSD 并将所有实例搜索/替换为“ xsd: ”(以及 xmlns:xsd= 定义)来解决此类问题。然后我在内部服务器上托管了这个 XSD,在 XML 中的 schemaLocation 属性中指向那里,一切正常。

由于措辞准确的错误,我尝试了这个:

  • 3) 文档的根元素不是<xsd:schema>

我知道,我知道,这不应该奏效,但它确实奏效了。没有翻译对我不起作用的外部 XSD 是http://camel.apache.org/schema/spring/camel-spring.xsd

我不确定这是否是 SAX 或 Eclipse 中的错误。

于 2014-03-17T23:51:08.897 回答
1

试试这个代码来验证你的 XSD,这个是避免你得到的异常的正确标准(这个是完整的证明代码)。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

    <xs:element name="commandCode"  type="xs:string"/>
    <xs:element name="code"  type="xs:string"/>
    <xs:element name="value" type="xs:string" />
    <xs:element name="customerID" type="xs:string" />
    <xs:element name="MessageType" type="xs:string" />
    <xs:element name="resultCode" type="xs:string" />
    <xs:element name="transactionInfo" type="xs:string" />

<xs:element name="mark" >
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="code" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="value" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="marks" >
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="mark" minOccurs="1" maxOccurs="unbounded" />
         <!--End element Mark -->
    </xs:sequence>
    </xs:complexType>
</xs:element> 
<xs:element name="commandLine">
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element ref="commandCode"  minOccurs="1" maxOccurs="1"/>
            <xs:element ref="marks" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="customerID" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="MessageType"  minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="statusInfo">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="transactionInfo" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="Antwoordregel" >
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="1">
            <xs:element ref="resultCode" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="statusInfo" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>  
    </xs:complexType>
</xs:element>

<!-- First Content Document Element -->
<xs:element name="Content">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="commandLine" minOccurs="1" maxOccurs="1" />
                <xs:element ref="Antwoordregel" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>
于 2013-08-08T04:26:49.013 回答