Java 根据架构验证XML-找不到元素的声明

Java 根据架构验证XML-找不到元素的声明,java,xml,validation,xsd,Java,Xml,Validation,Xsd,我对XML验证还很陌生,所以很抱歉提出了一个基本问题。我有以下文件: <tincan xmlns="http://projecttincan.com/tincan.xsd"> <activities> <activity id="TestActivity" type="course"> <name lang="und">MyCourse</name> <description lang="und

我对XML验证还很陌生,所以很抱歉提出了一个基本问题。我有以下文件:

<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>
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>

您没有向我们显示模式,因此我们只能猜测出哪里出了问题,但我的猜测是元素在错误的命名空间中声明


如果这是一个错误的猜测,那么下次当你问一个问题时,请记住,当人们不向你展示代码时,很难在代码中找到bug。

你没有向我们展示模式,因此我们只能猜测出什么是错的,但我的猜测是元素在错误的命名空间中声明


如果这是一个错误的猜测,那么下次当你问一个问题时,试着记住,当人们不向你展示代码时,很难在代码中发现错误。

让你的文档生成器“名称空间感知”应该可以修复它

  DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();          
  f.setNamespaceAware(true);
您还可以直接对源对象进行操作,这会使代码稍微短一点,而且速度更快,占用的内存更少

两种变体的样本
让您的文档生成器“namespaceAware”应该可以修复它

  DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();          
  f.setNamespaceAware(true);
您还可以直接对源对象进行操作,这会使代码稍微短一点,而且速度更快,占用的内存更少

两种变体的样本
我的问题中引用了该模式。我只是没有包括代码,因为它很长。您可以在这里找到:模式在xml文件
中引用,问题应该是自包含的,因此(a)当有人在几年后提出类似问题时,它们仍然有意义,以及(b)因此,我们这些对陌生人提供的链接持谨慎态度的人不必冒险。这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-@鉴于帖子提供的信息非常有限,这是一个合理的猜测。我的问题中引用了这个模式。我只是没有包括代码,因为它很长。您可以在这里找到:模式在xml文件
中引用,问题应该是自包含的,因此(a)当有人在几年后提出类似问题时,它们仍然有意义,以及(b)因此,我们这些对陌生人提供的链接持谨慎态度的人不必冒险。这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-@sidgate鉴于帖子提供的信息非常有限,这是一个合理的猜测。谢谢。工作起来很有魅力。请注意,使用StreamSource不仅使代码更短,而且可能更快,占用更少的内存。谢谢。工作起来很有魅力。注意,使用StreamSource不仅使代码更短,而且可能更快,占用更少的内存。
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);
    }
}