0

I'm trying to find a way to validate a XHTML 1.1 document. I have a method that validates XML against XSD and another against DTD.

The DTD one works with XHTML 1.0 Strict but runs forever with XHTML 1.1:

public static boolean validateAgainstDTD(String xhtml) {
        try {
                DocumentBuilderFactory factory = DocumentBuilderFactory
                                .newInstance();
                factory.setValidating(true);
                DocumentBuilder builder = factory.newDocumentBuilder();
                builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
                        // Ignore the fatal errors
                        @Override
                        public void fatalError(SAXParseException exception)
                                        throws SAXException {
                        }

                        // Validation errors
                        @Override
                        public void error(SAXParseException e) throws SAXParseException {
                                System.out.println("Error at " + e.getLineNumber()
                                                + " line.");
                                System.out.println(e.getMessage());
                                System.exit(0);
                        }

                        // Show warnings
                        @Override
                        public void warning(SAXParseException err)
                                        throws SAXParseException {
                                System.out.println(err.getMessage());
                                System.exit(0);
                        }
                });
                Document xmlDocument = builder.parse(new ByteArrayInputStream(xhtml.getBytes()));
                DOMSource source = new DOMSource(xmlDocument);
                StreamResult result = new StreamResult(System.out);
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer transformer = tf.newTransformer();
                transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                                "xhtml11.dtd");
                transformer.transform(source, result);
                return true;
        } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
        }
}

and the other one always says that it is not valid even if I take the example from the W3C website:

try {
       // parse an XML document into a DOM tree
       DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       Document document = parser.parse(new ByteArrayInputStream(str.getBytes()));

       // create a SchemaFactory capable of understanding WXS schemas
       SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

       // load a WXS schema, represented by a Schema instance: the XHTML 1.0 WXS schema
       Source schemaFile = new StreamSource("http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd");
       Schema schema = factory.newSchema(schemaFile);

       // create a Validator instance, which can be used to validate an instance document
       Validator validator = schema.newValidator();

       // validate the DOM tree
       validator.validate(new DOMSource(document));
  } catch (ParserConfigurationException e) {
       // parser configuration excepion
      System.out.println(e.getMessage());
  } catch (IOException e) {
       // io exception
      System.out.println(e.getMessage());
  } catch (SAXException e) {
       // instance document is invalid!
      System.out.println(e.getMessage());
  }

How do I validate XHTML 1.1 in Java?

I showed what I tried and nothing worked.

Does anyone see a mistake on my part, or have a solution?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
rXp
  • 617
  • 2
  • 12
  • 25
  • And the question is ... ? – C. M. Sperberg-McQueen May 09 '14 at 22:15
  • Title : How to validate XHTML 1.1 in Java ? I showed what I tried and nothing worked. Does anyone see a mistake on my part or have a solution ? – rXp May 12 '14 at 13:31
  • 1
    You say it works with XHTML 1.0 but "runs forever" with XHTML 1.1. Could that be caused by the fact that loading the DTD from the W3C website takes forever? I found that this is a very slow process, with including all the modular sub-files and such. You would be better off using this one: http://www.w3.org/TR/xhtml11/DTD/xhtml11-flat.dtd which is one big file without includes. Save it to your harddisk and use the local copy to validate against. – Mr Lister May 25 '14 at 09:09

1 Answers1

0

Use the following process:

  • Convert the DTD to an XML Serialization
  • Convert the XML Serialization to XSD
  • Retry with the local XSD

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265