1

We have two schema (XSD) files and one file include the other one. When we load the schema file to validate the XML files it is not properly loading in the web-application. It is throwing the error for included schema file elements.

        Source[] sources = new StreamSource[2];

            Source schemaFile = new StreamSource(Test.class.getClassLoader().getResourceAsStream(“a.xsd”));
            sources[0] = schemaFile;

            Source schemaFile1 = new StreamSource(Test.class.getClassLoader().getResourceAsStream(“b.xsd”));
            sources[1] = schemaFile1;

            Schema schema = factory.newSchema(sources);

b.xsd include the a.xsd file. But the same code is working fine when we run it in main method.

Could someone give suggestion to fix this issue?

We can do this with LSResourceResolver in org.w3c.dom.ls package. Problem validating an XML file using Java with an XSD having an include

Is there are any way to do this with javax in web base application?

Error:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns:Request'.
       at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
       at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
       at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
       at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
       at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
       at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
       at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
       at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
       at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
       at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
       at javax.xml.validation.Validator.validate(Unknown Source)
Community
  • 1
  • 1
user3496599
  • 1,207
  • 2
  • 12
  • 28

1 Answers1

1

The LSResourceResolver is a good one, using the whole xml resolving infrastructure.

The simple hack would be:

  • instead of using streams from class loader, copying both of your schema files to temporal location
  • use the File paths of the copied files instead

Or much easier

  • store your schemas not in the src folder next to the classes, but under web folder. You can obtain real file paths to the resources under the web and use them to initiate the schemas.
Zielu
  • 8,312
  • 4
  • 28
  • 41