开发者

Validating XML files in Java against two schemas with same namespace

I have

  1. an XML document,
  2. base XSD file and
  3. extended XSD file.

Both XSD files have one namespace.

File 3) includes file 2): <xs:include schemaLocation="someschema.xsd"></xs:include>

XML document (file 1) has following root tag:

<tagDefinedInSchema xmlns="http://myurl.com/myapp/myschema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://myurl.com/myapp/myschema schemaFile2.xsd">

where schemaFile2.xsd is the file 3 above.

I need to validate file 1 against both schemas, without

  1. modifying the file itself and

  2. merging two schemas in one file.

How can I do this in Java?

UPD: Here is the code I'm using.

Sc开发者_运维问答hemaFactory schemaFactory = SchemaFactory
        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
DocumentBuilderFactory documentFactory = DocumentBuilderFactory
        .newInstance();
documentFactory.setNamespaceAware(namespaceAware);
DocumentBuilder builder = documentFactory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xmlData
        .getBytes("UTF-8")));

File schemaLocation = new File(schemaFileName);
Schema schema = schemaFactory.newSchema(schemaLocation);

Validator validator = schema.newValidator();

Source source = new DOMSource(document);

validator.validate(source);

UPD 2: This works for me:

    public static void validate(final String xmlData,
        final String schemaFileName, final boolean namespaceAware)
        throws SAXException, IOException {
    final SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(new MySchemaResolver());
    final Schema schema = schemaFactory.newSchema();

    final Validator validator = schema.newValidator();
    validator.setResourceResolver(schemaFactory.getResourceResolver());

    final InputSource is = new InputSource(new ByteArrayInputStream(xmlData
            .getBytes("UTF-8")));
    validator.validate(new SAXSource(is), new SAXResult(new XMLReaderAdapter()));
}



class MySchemaResolver implements LSResourceResolver {

@Override
public LSInput resolveResource(final String type,
        final String namespaceURI, final String publicId, String systemId,
        final String baseURI) {
    final LSInput input = new DOMInputImpl();
    try {
        if (systemId == null) {
            systemId = SCHEMA1;
        }
        FileInputStream fis = new FileInputStream(
                new File("path_to_schema_directory/" + systemId));

        input.setByteStream(fis);
        return input;
    } catch (FileNotFoundException ex) {
        LOGGER.error("File Not found", ex);
        return null;
    }

}

}


A bit of terminology: you have one schema here, which is built from two schema documents.

If you specify schemaFile2.xsd to the API when building the Schema, it should automatically pull in the other document via the xs:include. If you suspect that this isn't happening, you need to explain what the symptoms are that cause you to believe this.


It may seem a bit inefficient, but couldn't you validate against schema A, create a new validator using schema B and validate against that one too?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜