简体   繁体   English

XSD验证错误:找不到元素'xs:schema'的声明

[英]XSD validation error: Cannot find the declaration of element 'xs:schema'

I saw that this question was asked many times but I didn't find a solution to my problem. 我看到这个问题被问过很多次,但是我没有找到解决问题的方法。 So the error is: 所以错误是:

Error on line 2 of document file...doc.xsd: cvc-elt.1: Cannot find the declaration of element 'xs:schema' 文档文件第2行出现错误... doc.xsd:cvc-elt.1:找不到元素'xs:schema'的声明

The code in the XSD file : XSD文件中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Collection" >
    <xs:complexType>
    <xs:sequence>
        <xs:element name="Description" type="xs:string"/>
        <xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Title" type="xs:string"/>
                    <xs:element name="Ingredients">
                        <xs:complexType>
                            <xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:attribute name="name" type="xs:string" use="required"/>
                                    <xs:attribute name="amount" type="xs:integer" use="required"/>
                                    <xs:attribute name="unit" type="xs:string" use="required"/>
                                </xs:complexType>
                            </xs:element>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="Preparation">
                        <xs:complexType>
                            <xs:element name="Step" type="xs:string minOccurs="0" maxOccurs="unbounded"/>"
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="Comment" type="xs:string" minOccurs="0"/>
                    <xs:element name="Nutrients">
                        <xs:complexType>
                            <xs:attribute name="proteins" type="xs:integer" use="required"/>
                            <xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
                            <xs:attribute name="fat" type="xs:integer" use="choice"/>
                            <xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
                            <xs:attribute name="calories" type="xs:float" use="required"/>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

The code in the XML file : XML文件中的代码:

<?xml version="1.0" encoding="UTF-8"?>

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="src/doc.xsd">
<Description>Nu prea merge</Description>
<Recipe>
<Title>Paste</Title>
<Ingredients>
    <Ingredient name="paste" amount="1" unit="pachet"></Ingredient>
</Ingredients>
<Preparation>
    <Step>Fierbere</Step>
    <Step>Servire</Step>
</Preparation>
<Nutrients proteins="2" carbohidrati="2" fat="da" vitamins="4" calories="1000.9"></Nutrients>
</Recipe>
</Collection>

The code in my Java class: 我的Java类中的代码

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

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public class Main {
    public static void main(String[] args) {
        File newFile = new File("src/doc.xsd");
        Document doc = null;

        SAXBuilder sbd = new SAXBuilder(XMLReaders.XSDVALIDATING);

        try {

            doc = sbd.build(newFile);
        } catch (JDOMException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        XMLOutputter outputDoc = new XMLOutputter();
        outputDoc.setFormat(Format.getPrettyFormat());
        try{
            outputDoc.output(doc,System.out);
            //outputDoc.output(doc, new FileWriter("src/myXmlDoc2.xml"));
        }
        catch(Exception e){
            System.out.println("Eroare la parsarea documentului XML!");
        }
}

}

If anyone has any idea about the possible problem please help me! 如果有人对可能的问题有任何想法,请帮助我!

Assuming that your XML is fixed, then you should change your XSD as follows: 假设您的XML是固定的,那么您应该按如下所示更改XSD:

  1. Fix the extra " as mentioned by @Andreas. 修正@Andreas提到的多余的"
  2. Add xs:sequence under xs:complexType in two places. xs:complexType的两个位置添加xs:sequence
  3. Change the type of @fat to xs:string . @fat的类型更改为xs:string
  4. Change use="choice" to use="optional" . use="choice"更改为use="optional"

Altogether, this XSD will successfully validate your XML: 总之,此XSD将成功验证您的XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Collection" >
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Description" type="xs:string"/>
        <xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Title" type="xs:string"/>
              <xs:element name="Ingredients">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute name="name" type="xs:string" use="required"/>
                        <xs:attribute name="amount" type="xs:integer" use="required"/>
                        <xs:attribute name="unit" type="xs:string" use="required"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="Preparation">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Step" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="Comment" type="xs:string" minOccurs="0"/>
              <xs:element name="Nutrients">
                <xs:complexType>
                  <xs:attribute name="proteins" type="xs:integer" use="required"/>
                  <xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
                  <xs:attribute name="fat" type="xs:string" use="optional"/>
                  <xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
                  <xs:attribute name="calories" type="xs:float" use="required"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Of course, you will also have to check your Java code. 当然,您还必须检查Java代码。 (Hint: make sure you're not validating your XSD as XML when you really want to validating your XML against your XSD.) (提示:当您确实要针对XSD验证XML时,请确保未将XSD验证为XML。)

暂无
暂无

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

相关问题 无法通过代码找到简单模式验证的元素错误声明 - Cannot find the declaration of element error for simple schema validation via code 使用cvc-elt.1进行xsd验证失败:无法找到元素的声明 - xsd validation fails with cvc-elt.1: Cannot find the declaration of element XML 架构验证:找不到元素(根)的声明 - XML Schema Validation : Cannot find the declaration of element (root) CXF的JAXB模式验证错误-发生JAXBException:cvc-elt.1:找不到元素的声明 - JAXB schema validation error with CXF - JAXBException occurred : cvc-elt.1: Cannot find the declaration of element 未定义的元素声明 &#39;xs:schema&#39; - undefined element declaration 'xs:schema' 无法针对xsd模式验证xml文档(找不到元素&#39;replyMessage&#39;的声明) - Cannot validate xml doc against a xsd schema (Cannot find the declaration of element 'replyMessage') 无法针对 xsd 找到元素 xml 的声明 - Cannot find the declaration of element xml against xsd Jaxb2Marshaller无法通过xsd模式序列化POJO:“找不到元素的声明” - Jaxb2Marshaller can not serialize POJO by xsd schema: “cannot find the declaration of element” 针对XSD的XML验证:cvc-elt.1:找不到元素&#39;xxx&#39;的声明 - XML validation against XSD: cvc-elt.1: Cannot find the declaration of element 'xxx' 无法引用另一个架构:无法将名称“xs:架构”解析为(n)“元素声明”组件 - Unable to ref to another schema: Cannot resolve the name 'xs:schema' to a(n) 'element declaration' component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM