How to validate XML against the XSD Schema containing import without schema-location?
Fragment of XSD:
<xs:schema xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types
xmlns:tns="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/exchange/services/2006/types"
elementFormDefault="qualified" version="Exchange2010_SP2" id="types">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
...
Already read and tried:
This one and this too... Unsuccessfully.
Cannot remove this import from schema, because it contains reference of xml:lang attribute.
In variant 1 ResourceResolver resolveResource method fired with systemId = null
public class ResourceResolver implements LSResourceResolver {
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
//Some implementation
return new Input(publicId, systemId, resourceAsStream);
In variant 2 tried like this:
SchemaFactory sFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//sFactory.setResourceResolver(new ResourceResolver());
Schema schema = sFactory.newSchema(new Source[] {
new StreamSource("http://www.w3.org/XML/1998/namespace"),
new StreamSource(MailGateMQBinding.class.getResourceAsStream("/types.xsd")),
});
validator = messageSchema.newValidator();
source = new DOMSource(inDocBody);
validator.validate(source);
But have an Exception:
without new StreamSource("http://www.w3.org/XML/1998/namespace")
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'xml:lang' to a(n) 'attribute declaration'.
and with this new StreamSource("http://www.w3.org/XML/1998/namespace")
org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'.. Saw 'The "xml:" Namespace'.
Any help would be greatly appreciated.
You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.
Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.
The XML schema for http://www.w3.org/XML/1998/namespace
namespace is located here:
https://www.w3.org/2009/01/xml.xsd
So, you can just specify its location in <xs:import>
in your schema:
<xs:schema xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types
xmlns:tns="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/exchange/services/2006/types"
elementFormDefault="qualified" version="Exchange2010_SP2" id="types">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="https://www.w3.org/2009/01/xml.xsd"/>
...
That will work, but note that W3C doesn't like huge traffic to that file: http://www.w3.org/2001/xml.xsd. So, they delay artificially the access to it.
Many software hold local copies of such schemas. (That's why the schema location is not specified. The schema software typically loads it from its resources).
You may also copy it to you computer and specify the URL to that copy.
An alternative way is to use XML catalog, like this (catalog.xml):
<?xml version="1.0"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<!--
This will redirect the namespace URI to the local schema file,
which should be found in the same directory as the catalog.xml
-->
<uri name="http://www.w3.org/XML/1998/namespace" uri="xml.xsd"/>
</catalog>
But you will have to pass somehow that catalog file to your schema processor software (if it supports XML catalogs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With