1

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 getting 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'.

рüффп
  • 5,172
  • 34
  • 67
  • 113
Jaydeep Patel
  • 2,394
  • 1
  • 21
  • 27
  • Actually, what **is** going wrong? – tangens May 18 '10 at 06:48
  • I have updated question with error information. – Jaydeep Patel May 18 '10 at 07:25
  • @user279554 download altova xml spy and you'll get error there if there is an error in your xsd, if xsd schema is valid then generate xml from it and parse it or whatever from java. – ant May 18 '10 at 07:31
  • possible duplicate of [Problem validating an XML file using Java with an XSD having an include](http://stackoverflow.com/questions/2342808/problem-validating-an-xml-file-using-java-with-an-xsd-having-an-include) – Aravind Yarram Nov 14 '11 at 18:29

2 Answers2

2

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

anthavio
  • 101
  • 1
  • 4
0

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"/>
bakoyaro
  • 2,550
  • 3
  • 36
  • 63
Jeff
  • 1
  • 1
    Thought this was well, but apparently for an import you don't have to specify a schema location. The details can be found [here](http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/#composition-schemaImport). – G_H Nov 14 '11 at 18:24