开发者

Multiple schema validation in Java

开发者 https://www.devze.com 2022-12-30 11:10 出处:网络
I am trying to do multiple schema validation in Java. I don\'t understand where I am doing wrong. Any help will be appreciated.

I am trying to do multiple schema validation in Java. I don't understand where I am doing wrong. Any help will be appreciated.

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>

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

Test case

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

Edit

If I run above test case I am g开发者_JS百科etting below error 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'.


I guess that it is known xerces bug in namespace based schema caching https://issues.apache.org/jira/browse/XERCESJ-1130


I think you need to use the following XML:

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

instead of

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

精彩评论

暂无评论...
验证码 换一张
取 消