简体   繁体   English

根据模式验证XML-找不到元素的声明

[英]Validating XML against schema - cannot find declaration of element

I am quite new to XML validation so sorry for the elementary question. 我对XML验证非常陌生,因此很抱歉基本问题。 I have the following document: 我有以下文件:

<tincan xmlns="http://projecttincan.com/tincan.xsd">
  <activities>
    <activity id="TestActivity" type="course">
      <name lang="und">MyCourse</name>
      <description lang="und"/>
      <launch lang="und">start.html</launch>
    </activity>
    <activity id="p001" type="objective">
      <name lang="und">First Page</name>
      <description lang="und">First Page</description>
    </activity>
  </activities>
</tincan>

I am trying to validate it against the following schema: http://projecttincan.com/tincan.xsd (I also tried removing the schema from my XML and providing it externally). 我正在尝试根据以下架构对其进行验证: http : //projecttincan.com/tincan.xsd (我还尝试从XML中删除该架构并从外部提供它)。

I always get the following exception: cvc-elt.1: Cannot find the declaration of element 'tincan' 我总是收到以下异常: cvc-elt.1: Cannot find the declaration of element 'tincan'

By looking into the schema, I see that the tincan element is defined there and it is also present in my XML so I don't udnerstand what is the source of this exception. 通过查看模式,我发现在tincan定义了tincan元素,并且该元素也存在于我的XML中,因此我无法理解此异常的根源。 I would be glad if someone could explain how the validation is supposed to work. 如果有人可以解释验证的工作原理,我将感到非常高兴。

EDIT: My code for the validation: 编辑:我的验证代码:

DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(stream);

// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(getClass().getClassLoader().getResource("tincan.xsd").getFile());
Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();

// validate the DOM tree
validator.validate(new DOMSource(document));

EDIT2: Schema was linked here but I will post it here again for clarity: EDIT2:模式已链接到此处,但为了清楚起见,我将其再次发布在此处:

<xs:schema xmlns="http://projecttincan.com/tincan.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tc="http://projecttincan.com/tincan.xsd" targetNamespace="http://projecttincan.com/tincan.xsd" elementFormDefault="qualified">
<xs:element name="tincan" type="tincan"></xs:element>
<xs:complexType name="tincan">
<xs:sequence>
<xs:element name="activities" type="activities" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="activity">
<xs:sequence>
<xs:element name="name" type="langstring" maxOccurs="unbounded"/>
<xs:element name="description" type="langstring" maxOccurs="unbounded"/>
<xs:element name="launch" type="langURI" maxOccurs="unbounded" minOccurs="0"/>
<xs:element name="resource" type="langURI" maxOccurs="unbounded" minOccurs="0"/>
<xs:element name="interactionType" type="xs:string" minOccurs="0"/>
<!--  CMI Interaction fields  -->
<xs:element name="correctResponsePatterns" type="correctResponsePatternList" minOccurs="0"/>
<xs:element name="choices" type="interactionComponentList" minOccurs="0"/>
<xs:element name="scale" type="interactionComponentList" minOccurs="0"/>
<xs:element name="source" type="interactionComponentList" minOccurs="0"/>
<xs:element name="target" type="interactionComponentList" minOccurs="0"/>
<xs:element name="steps" type="interactionComponentList" minOccurs="0"/>
<!--  Extensions -->
<xs:element name="extensions" type="extensions" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:anyURI"/>
<xs:attribute name="type" type="xs:string"/>
</xs:complexType>
<xs:complexType name="activities">
<xs:sequence>
<xs:sequence>
<xs:element name="activity" type="activity" maxOccurs="unbounded"/>
<xs:element name="provider" type="provider" minOccurs="0"/>
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:complexType name="provider">
<xs:all>
<xs:element name="name" type="langstring"/>
<xs:element name="id" type="xs:string"/>
<xs:element name="secret" type="xs:string" minOccurs="0"/>
<xs:element name="public_key" type="xs:string" minOccurs="0"/>
<xs:element name="info" type="xs:anyURI" minOccurs="0"/>
</xs:all>
</xs:complexType>
<xs:complexType name="correctResponsePatternList">
<xs:sequence>
<xs:element name="correctResponsePattern" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="interactionComponentList">
<xs:sequence>
<xs:element name="component" type="interactionComponentType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="interactionComponentType">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="description" type="langstring" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="langstring">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="lang" type="xs:language"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="extensions">
<xs:sequence>
<xs:element name="extension" type="extension" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="extension">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="langURI">
<xs:simpleContent>
<xs:extension base="xs:anyURI">
<xs:attribute name="lang" type="xs:language"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>

Make your document builder "namespaceAware" should fix it! 使您的文档构建器“ namespaceAware”应该修复它!

  DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();          
  f.setNamespaceAware(true);

You can also operate directly on a Source object, which makes code a little bit shorter, it's also likely to be faster and use less memory. 您还可以直接在Source对象上进行操作,这会使代码短一些,它也可能更快并且使用更少的内存。

Sample with both variants 两种变体的样品

package stack43324079;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.junit.Test;
import org.w3c.dom.Document;

public class HowToValidateXml {
    @Test
    public void validate1() throws Exception {
        DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
        f.setNamespaceAware(true);
        DocumentBuilder parser = f.newDocumentBuilder();
        Document document = parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("43324079.xml"));
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaFile = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("tincan.xsd"));
        Schema schema = factory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    }

    @Test
    public void validate2() throws Exception {
        Source xmlFile = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("43324079.xml"));
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(Thread.currentThread().getContextClassLoader().getResource("tincan.xsd"));
        Validator validator = schema.newValidator();
        validator.validate(xmlFile);
    }
}

You're not showing us the schema, so we can only guess what's wrong, but my guess would be that the element is declared in the wrong namespace. 您没有向我们显示架构,所以我们只能猜测出了什么问题,但是我的猜测是该元素是在错误的命名空间中声明的。

If that's a wrong guess, then try to remember next time you ask a question that it's very hard to find bugs in code when people don't show you the code. 如果这是一个错误的猜测,请尝试记住下一次您提出的问题:当人们不向您显示代码时,很难在代码中发现错误。

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

相关问题 无法针对 xsd 找到元素 xml 的声明 - Cannot find the declaration of element xml against xsd 无法针对xsd模式验证xml文档(找不到元素&#39;replyMessage&#39;的声明) - Cannot validate xml doc against a xsd schema (Cannot find the declaration of element 'replyMessage') XML 架构验证:找不到元素(根)的声明 - XML Schema Validation : Cannot find the declaration of element (root) 根据模式验证XML文档 - validating a XML document against a schema cvc-elt.1:找不到元素&#39;staff&#39;的声明-无法针对xsd验证xml - cvc-elt.1: Cannot find the declaration of element 'staff' - Cannot validate xml against a xsd 针对XSD的XML验证:cvc-elt.1:找不到元素&#39;xxx&#39;的声明 - XML validation against XSD: cvc-elt.1: Cannot find the declaration of element 'xxx' 当我尝试使用 DocumentBuilderFactory 按模式验证 xml 文件时,我收到错误“找不到元素的声明” - When i try to validate xml file using DocumentBuilderFactory by schema i recieve error "Cannot find the declaration of element " 无法通过代码找到简单模式验证的元素错误声明 - Cannot find the declaration of element error for simple schema validation via code XSD验证错误:找不到元素&#39;xs:schema&#39;的声明 - XSD validation error: Cannot find the declaration of element 'xs:schema' 使用DocumentBuilder验证模式 - Validating against a schema with DocumentBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM