cvc-elt.1:找不到元素'NewIssue'的声明

问题描述:

我想在将它写入文件之前验证内存中的xml文档。我发现了许多类似的问题,但这里有一个区别,我会这么认为。 多模式是这个验证定义,“重新定义”选项,在相互之间的关系,从父母到孩子,如下: CoreSchema.xsd - > CenterSchema.xsd - > CenterSchema_REF.xsdcvc-elt.1:找不到元素'NewIssue'的声明

CoreSchema.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="1.8"> 
<xs:element name="NewIssue"> 
    <xs:annotation> 
     <xs:documentation>Root element to add new issues</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element name="NewIssueList"> 
       <xs:annotation> 
        <xs:documentation>Contains a list of issues</xs:documentation> 
       </xs:annotation> 
       <xs:complexType> 
        <xs:sequence maxOccurs="unbounded"> 
         <xs:element name="Issue" type="ImportIssueType"/> 
        </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 
... 
... 

CenterSchema.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://http://www.example.com/supplier" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.8"> 
<xs:redefine schemaLocation="CoreSchema.xsd"> 
    <xs:complexType name="Issue"> 
     <xs:complexContent> 
      <xs:extension base="Issue"> 
       <xs:sequence> 
        <xs:element name="Component" type="Components"/> 
        <xs:element name="Keywords" type="Keywords" minOccurs="0"/> 
        <xs:element name="SupplierStatus" type="SupplierStatus" minOccurs="0"/> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 
    <xs:complexType name="ImportIssueType"> 
     <xs:annotation> 
      <xs:documentation>Definition of a exported issue</xs:documentation> 
     </xs:annotation> 
     <xs:complexContent> 
      <xs:extension base="ImportIssueType"> 
       <xs:sequence> 
        <xs:element name="Component" type="Components"> 
         <xs:annotation> 
          <xs:documentation>Describes the component where the issue occurs.</xs:documentation> 
         </xs:annotation> 
        </xs:element> 
        <xs:element name="Keywords" type="Keywords" minOccurs="0"> 
         <xs:annotation> 
          <xs:documentation>Keyword applied for the issue.</xs:documentation> 
         </xs:annotation> 
        </xs:element> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 
... 
... 
</xs:redefine> 
</xs:schema> 

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> 

编辑:序列化的文件(写在.xml文件)看起来LIK E本:

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

我实现了在SchemaFactory和验证如下:

private boolean toFile(final String outputPath, final Node document) { 
    boolean resultOk = false; 
    try { 
     DOMSource domSource = new DOMSource(document);   
     SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
     factory.setResourceResolver(new CustomResourceResolver()); 

      try { 
       InputStream schemaCSREF = DxiParser.class.getResourceAsStream("/CenterSchema_REF.xsd"); 
       factory.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true); 
       factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);      
       Source source = new StreamSource(schemaCSREF); 
       Schema schema = factory.newSchema(source); 
       Validator validator = schema.newValidator(); 
       validator.validate(domSource); 
      } catch (SAXException e) { 
       // instance document is invalid! 
       System.out.println(e.getLocalizedMessage());    
       System.out.println("\n** SAX Parser: Error during validation of " +document.getNodeName()); 
       return 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(); 
      resultOk = true; 

     // ---- Error handling ---- 
    } catch (TransformerConfigurationException tce) { 
     System.out.println("\n** Transformer Factory error"); 
     System.out.println(" " + tce.getMessage()); 
     Throwable e = (tce.getException() != null) ? tce.getException() 
       : tce; 
     e.printStackTrace(); 
    } catch (TransformerException tfe) { 
     System.out.println("\n** Transformation error"); 
     System.out.println(" " + tfe.getMessage()); 
     Throwable e = (tfe.getException() != null) ? tfe.getException() 
       : tfe; 
     e.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 

    return resultOk; 
} 

CustomResource解析器实现这样的:

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的阵列加载这个特定条目:

复杂类型名称=“http://www.example.com/supplier,#AnonType_NewIssue”,基本类型名称='anyType',content type ='ELEMENT',isAbstract ='false',hasTypeId ='false',final ='0',block ='0',particle ='(“http://www.example.com/supplier”:NewIssueList)',derivedBy = '限制'。

所以这证明CoreSchema已通过CenterSchema通过CenterSchema_REF到达。 注意:当我将出厂特征“http://apache.org/xml/features/validation/schema-full-checking”设置为true时,XSComplexTypeDecl字段为空。

试图将所有3个xsds添加为Source [],错误是相同的。尝试将不同的工厂特征设置为true/false。

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

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

+0

不清楚你的问题到底是什么。内存中的文档(如同您复制的那样)格式不正确(“协议”的结束标记似乎不匹配任何开始标记)。 – 2014-08-28 14:32:51

+0

编辑完成后:谢谢Sperberg先生指出了这一点。这实际上是一个复制粘贴错误。 XML格式正确(我在其他XML商业验证程序中检查了这组3个模式,并且没有问题。) 问题在问题标题中 - 为什么我无法检测元素声明,如果它是通过CustomResourceResolver加载的? – bstefano 2014-08-28 14:38:23

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

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

不过,这不是根据需要,但至少我不会堵塞,可以管理。