Piyush
Piyush

Reputation: 372

Validating xml against an xsd which has an include

I'm trying to validate an XML against an XSD which internally refers to another XSD (using include statement).

as,

<xs:include schemaLocation="Schema2.xsd"/>

Now while validating my XML against the xsd(schema1.xsd) like:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();

        Document document = builder.parse(new InputSource(new StringReader(
                inputXml)));
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(document);
        Result xmlFile = new StreamResult(new File("xmlFileName.xml"));
        aTransformer.transform(src, xmlFile);
    } catch (Exception e) {
        // TODO: handle exception
    }

    File xmlFile = new File("xmlFileName.xml");
    SchemaFactory factory1 = SchemaFactory
            .newInstance("http://www.w3.org/2001/XMLSchema");

    File schemaLocation = new File("F:\\Project\\Automation\\XSDs\\schema1.xsd");
    Schema schema = factory1.newSchema(schemaLocation);

    Validator validator = schema.newValidator();

    Source source = new StreamSource(xmlFile);

    try {
        validator.validate(source);
        System.out.println(xmlFile.getName() + " is valid.");
        isXmlValid = true;
    } catch (SAXException ex) {
        System.out.println(xmlFile.getName() + " is not valid because ");
        System.out.println(ex.getMessage());
        isXmlValid = false;
    }

I get an error, "cvc-datatype-valid.1.2.1: 'True' is not a valid value for 'boolean'."

This is for an element which is defined in the schema2.xsd which schema1.xsd is referring.

Please tell me if I am doing something wrong.

Upvotes: 0

Views: 1873

Answers (1)

dcbyers
dcbyers

Reputation: 864

You are experiencing a case problem. 'true' is a valid value for xsd:boolean, but 'True' (which is apparently what is in the XML document) is not.

The responses here (xsd:boolean element type accept "true" but not "True". How can I make it accept it?) provide some additional information and a possible alternate solution based on enumerated values (if you cannot change the True to true).

Upvotes: 1

Related Questions