Java中的多个模式验证

时间:2010-05-18 06:47:29

标签: java xml validation xsd schema

我正在尝试在Java中进行多个模式验证。我不明白我做错了什么。任何帮助将不胜感激。

abc.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xn="project-xml-r4j_another.xsd">
 <xsd:import namespace="project-xml-r4j_another.xsd"/>
 <xsd:element name="abc" type="abc">
 </xsd:element>
 <xsd:complexType name="abc">
  <xsd:sequence>
   <xsd:element name="test" type="test" minOccurs="0" maxOccurs="1">
   </xsd:element>
   <!--<xsd:element name="proj" type="xn:proj"/>-->
  </xsd:sequence>
  <xsd:attribute name="id" type="xsd:ID" use="required"/>
 </xsd:complexType>
 <xsd:complexType name="test">
  <xsd:attribute name="id" type="xsd:ID" use="required"></xsd:attribute>
  <xsd:attribute name="value" use="required">
   <xsd:simpleType>
    <xsd:restriction base="xsd:string">
     <xsd:maxLength value="100" />
    </xsd:restriction>
   </xsd:simpleType>
  </xsd:attribute>
 </xsd:complexType>
</xsd:schema>

项目的XML-r4j_another.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="project-xml-r4j_another.xsd" xmlns="project-xml-r4j_another.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xsd:element name="proj" type="proj">
  <xsd:annotation>
   <xsd:documentation>
    The project is the root tag of a project-xml.
   </xsd:documentation>
  </xsd:annotation>
 </xsd:element>
 <xsd:complexType name="proj">
  <xsd:attribute name="id" type="xsd:ID" use="required"/>
 </xsd:complexType>
</xsd:schema>

测试用例

package test;

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

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import com.ericsson.ccrtool.core.project.projectxml.InvalidProjectXmlException;

public class TestSchema {
 private static final Logger logger = Logger.getLogger(TestSchema.class);
 static final String W3C_XML_SCHEMA = XMLConstants.W3C_XML_SCHEMA_NS_URI;
 @Test
 public void test() {
  System.out.println("TestSchema.test()");
  try {

   SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA);
   // create a grammar object.
   Source [] source = { new StreamSource(new File("C:\\jaydeep\\Ericsson\\R5B\\abc.xsd")),
     new StreamSource(new File("C:\\jaydeep\\Ericsson\\R5B\\project-xml-r4j.xsd"))};
   Schema schemaGrammar = schemaFactory.newSchema(source);

   Validator schemaValidator = schemaGrammar.newValidator();
   schemaValidator.setErrorHandler(new MessageHandler());

   // validate xml instance against the grammar.
   schemaValidator.validate(new StreamSource("C:\\jaydeep\\Ericsson\\R5B\\project_tmmk17cells_xnaveen_project-xml.xml"));

  } catch (SAXException e) {
   throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + e.getMessage(), e);
  } catch (IOException e) {
   throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + e.getMessage(), e);
  }
 }
 class MessageHandler extends DefaultHandler {
  private String errMessage = "";

  @Override
  public void warning(SAXParseException e) {
   logger.info("Warning Line " + e.getLineNumber() + ": " + e.getMessage());
  }

  @Override
  public void error(SAXParseException e) {
   errMessage = new String("Error Line " + e.getLineNumber() + ": " + e.getMessage());
   logger.info(errMessage);
   throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + errMessage);
  }

  @Override
  public void fatalError(SAXParseException e) {
   errMessage = new String("Error Line " + e.getLineNumber() + ": " + e.getMessage());
   logger.info(errMessage);
   throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + errMessage);
  }
 }
}

修改

如果我在测试用例之上运行,则会出现以下错误com.ericsson.ccrtool.core.project.projectxml.InvalidProjectXmlException: Project-xml validation failed, Exception: Error Line 2: cvc-elt.1: Cannot find the declaration of element 'abc'.

2 个答案:

答案 0 :(得分:2)

我猜在基于命名空间的架构缓存中已知xerces bug https://issues.apache.org/jira/browse/XERCESJ-1130

答案 1 :(得分:0)

我认为您需要使用以下XML:

<xsd:import namespace="project-xml-r4j_another.xsd" schemaLocation="project-xml-r4j_another.xsd" />

而不是

<xsd:import namespace="project-xml-r4j_another.xsd"/>