3

I'm trying to validate the following XML

<query>
 <colors logic="AND">
  <color main="BLUE" tone="DARK" operator="=" />
 </colors>
</query>

using the following XSD

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:complexType name="color">
     <xsd:attribute name="main" type="xsd:string" use="required"/>
     <xsd:attribute name="tone" type="xsd:string" use="required"/>
     <xsd:attribute name="operator" type="xsd:string"/>
 </xsd:complexType>
 <xsd:complexType name="colors">
     <xsd:sequence>
         <xsd:element name="color" type="color" maxOccurs="unbounded">   </xsd:element>
     </xsd:sequence>
     <xsd:attribute name="logic" type="xsd:string" use="required"/>
 </xsd:complexType>
 <xsd:complexType name="query">
     <xsd:sequence>
         <xsd:element name="colors" type="colors" maxOccurs="2"></xsd:element>
     </xsd:sequence>
 </xsd:complexType>
 <xsd:element name="query" type="query"></xsd:element>
</xsd:schema>

So... I want to validate the XML without any namespace. I can't change the XML since it is generated by another application, I only want guarantee, on the server side, that the client is sending the right request.

When I try to validate the XML against the XSD I get the following exception message:

cvc-elt.1: Cannot find the declaration of element 'query'

I already searched and came into solutions like this and this but with no success

Solution (thanks @Traroth for pointing me in the right direction)---

Here's how I'm validating it:

I have this function:

public static Document buildValidRequest(String content, Schema xsd) throws SAXParseException, SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);
    factory.setNamespaceAware(true);

    factory.setSchema(xsd);

    XMLSimpleErrorHandler errorHandler = new XMLSimpleErrorHandler();

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(errorHandler);

    StringReader reader = new StringReader(content);

    Document validXML = builder.parse(new InputSource(reader));

    if (errorHandler.getException() != null) {
        throw errorHandler.getException();
    }

    return validXML;

}

And this class to handle the errors:

public class XMLSimpleErrorHandler implements ErrorHandler {

private SAXParseException exception;

@Override
public void warning(SAXParseException e) {
    this.exception = e;
}

@Override
public void error(SAXParseException e) {
    this.exception = e;
}

@Override
public void fatalError(SAXParseException e) {
    this.exception = e;
}

public SAXParseException getException() {
    return exception;
}
}

And this method to get the Schema:

private static Schema getSchema(String xsdPath) throws SAXException, IOException {
    InputStream resourceAsStream = null;
    try {
        ServiceManager.getInstance().getLoggerManager().debug(RESTInitServlet.LOGCONTEXT, TAG, "Retrieving schema: "+xsdPath);
        resourceAsStream = getInstance().getClass().getResourceAsStream(xsdPath);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaSource = new StreamSource(resourceAsStream);
        Schema schema = schemaFactory.newSchema(schemaSource);
        return schema;
    } finally {
        if (resourceAsStream != null) {
            resourceAsStream.close();
        }
    }

}

Nevermind this: Weirdest thing of all: works on Tomcat 6 running on Windows 7; not working in jboss running on Linux...

Community
  • 1
  • 1
Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74
  • Helping you will be difficult without seeing any code. – user845279 Jun 21 '12 at 14:32
  • @user845279 I thought that the XML / XSD could have some issues so I didn't had the code.. Now it is there what I'm using to validate – Miguel Ribeiro Jun 21 '12 at 15:39
  • That's true but there is a higher chance the error is in your code so it never hurts to look into that first. Thanks for the updated post. – user845279 Jun 21 '12 at 15:59
  • Try this example: http://java-by-ash.blogspot.com/2012/07/xml-schema-validation.html –  Jul 20 '12 at 11:13

3 Answers3

1

You should find your answer here: http://www.edankert.com/validate.html#Validate_using_external_Schema

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
1

It may be a long shot, but I remember that when we used JBoss 5.0 or .1 we found a serious bug in the used xerces implementation. Please check the version for bugs or just try a newer version of the xerces library.

keiki
  • 3,260
  • 3
  • 30
  • 38
0

Your instance document and schema document look fine; there's something wrong in the way you are invoking the validation.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164