Java cvc elt.1:找不到元素';新发行';

Java cvc elt.1:找不到元素';新发行';,java,xml,xsd,saxparser,redefine,Java,Xml,Xsd,Saxparser,Redefine,我试图在将内存中的xml文档写入文件之前验证它。我发现了许多与我的问题相似的问题,但这里有一个区别,我想是的。 为此验证定义了多个架构,并在从父到子的关系中使用“重定义”选项,如下所示: CoreSchema.xsd->CenterSchema.xsd->CenterSchema_REF.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns="http://www.example.com/supplier" xm

我试图在将内存中的xml文档写入文件之前验证它。我发现了许多与我的问题相似的问题,但这里有一个区别,我想是的。 为此验证定义了多个架构,并在从父到子的关系中使用“重定义”选项,如下所示: CoreSchema.xsd->CenterSchema.xsd->CenterSchema_REF.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.example.com/supplier" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/supplier" elementFormDefault="qualified" attributeFormDefault="unqualified" version="4.7">
<xs:redefine schemaLocation="CenterSchema.xsd">
...
...
</xs:redefine>
CoreSchema.xsd(由于安全策略,仅示例,不完整)

CustomResourceResolver的实现方式如下:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<NewIssue xmlns="http://www.example.com/supplier" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/supplier CenterSchema_REF.xsd">
<NewIssueList>    
<Issue>
  <SupplierID>574</SupplierID>
  <NewIssueMode>Read Only</NewIssueMode>
  <Author>
    <Name/>
  </Author>
  <Category>Software</Category>
  <ItemType>Test-Issue</ItemType>
  <IssueClass>Issue</IssueClass>
  <DetectedOnDate>2014-08-14</DetectedOnDate>
  <Device>TEST</Device> 
  <Severity>1</Severity>
  <Supplier>
    <ContactName/>
    <Data>Analysis: [Reason of problem] [Condition for defect] [Impact] [Risk] [Root cause]</Data>
    <Status>Supplier Not Assigned</Status>
    <StatusInternal>SUBMITTED</StatusInternal>
  </Supplier>
...
... 
</Issue>
</NewIssueList>
</NewIssue>
public class CustomResourceResolver implements LSResourceResolver {

@Override
public LSInput resolveResource(String type, String namespaceURI,
        String publicId, String systemId, String baseURI) {

    LSInputImpl input = new LSInputImpl();      
    InputStream stream = null;
    try {           
        stream = new FileInputStream(new File(systemId));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    input.setPublicId(publicId);
    input.setSystemId(systemId);
    input.setBaseURI(baseURI);
    input.setCharacterStream(new InputStreamReader(stream));

    return input;

}

}

LSInput实现是标准的:

public class LSInputImpl implements LSInput{

private Reader characterStream;
private InputStream byteStream;
private String stringData;
private String systemId;
private String publicId;
private String baseURI;
private String encoding;
private boolean certifiedText;

//getters and setters
}

我可以确认所有模式文件都已加载(在路径中找到)并填充到模式对象中。我在Schema对象的语法字段中看到,所有复杂类型都被检测到,并且我看到XSComplexTypeDecl的数组中加载了这个特定条目:

复杂类型名称=“”,基本类型名称=“”,内容类型=“”,内容类型=“”,IsaStract=“”,hasTypeId=“”,final=“”,block=“”,particle=“”(“”:NewIssueList)“”,derivedBy=“”。

这证明了CoreSchema是通过CenterSchema访问的,而CenterSchema是通过CenterSchema\u REF访问的。 注意:当我将factory feature“”设置为true时,XSComplexTypeDecl字段为null

尝试将所有3个XSD添加为源[],错误相同。尝试将不同的factory功能设置为true/false

我不知道还要检查什么,完全卡住了


如果需要的话,我可以提供更多的信息。谢谢大家。

作为一种解决方法,我在序列化后通过从文件系统加载实例来实际验证实例,从而解决了这个问题

            try {   
            InputStream schemaCSAPC = new FileInputStream(schemaFile);
            factory.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);
            factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);                    

            FileOutputStream xmlOut = new FileOutputStream(new File(outputPath));
            StreamResult streamResult = new StreamResult(xmlOut);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            // serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.transform(domSource, streamResult);

            xmlOut.close();
            Source source = new StreamSource(new FileInputStream(outputPath));
            Source shemaSource = new StreamSource(schemaCSREF);
            Schema schema = factory.newSchema(shemaSource);
            Validator validator = schema.newValidator();
            validator.validate(source);
            } catch (SAXException e) {
                // instance document is invalid!            
                System.out.println("\n** SAX Parser: Error during validation of " +document.getNodeName());
                return false;
            }


尽管如此,这并不符合要求,但至少我没有被阻止,可以管理。

不清楚您的问题是什么。内存中的文档(当您复制它时)格式不正确(协议的结束标记似乎与任何开始标记都不匹配)。编辑后:谢谢Sperberg先生指出这一点。这实际上是一个复制粘贴错误。XML格式良好(我在其他XML商业验证器中对照这组3个模式检查它,这很好。)问题标题中有一个问题-如果元素声明是通过CustomResourceResolver加载的,为什么我无法检测到它?
public class CustomResourceResolver implements LSResourceResolver {

@Override
public LSInput resolveResource(String type, String namespaceURI,
        String publicId, String systemId, String baseURI) {

    LSInputImpl input = new LSInputImpl();      
    InputStream stream = null;
    try {           
        stream = new FileInputStream(new File(systemId));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    input.setPublicId(publicId);
    input.setSystemId(systemId);
    input.setBaseURI(baseURI);
    input.setCharacterStream(new InputStreamReader(stream));

    return input;

}

}
public class LSInputImpl implements LSInput{

private Reader characterStream;
private InputStream byteStream;
private String stringData;
private String systemId;
private String publicId;
private String baseURI;
private String encoding;
private boolean certifiedText;

//getters and setters
}
            try {   
            InputStream schemaCSAPC = new FileInputStream(schemaFile);
            factory.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);
            factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);                    

            FileOutputStream xmlOut = new FileOutputStream(new File(outputPath));
            StreamResult streamResult = new StreamResult(xmlOut);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            // serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.transform(domSource, streamResult);

            xmlOut.close();
            Source source = new StreamSource(new FileInputStream(outputPath));
            Source shemaSource = new StreamSource(schemaCSREF);
            Schema schema = factory.newSchema(shemaSource);
            Validator validator = schema.newValidator();
            validator.validate(source);
            } catch (SAXException e) {
                // instance document is invalid!            
                System.out.println("\n** SAX Parser: Error during validation of " +document.getNodeName());
                return false;
            }