XML validation on XSD containing xsd: import without location

How to check XML for XSD schema containing import without schema location?

XSD snippet:

<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 one too ... Unsuccessful.

This import cannot be removed from the schema because it contains a reference to the xml: lang attribute.

In option 1, ResourceResolver resolveResource method launched using 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 option 2 I 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 there is an exception: without new StreamSource("http://www.w3.org/XML/1998/namespace") org.xml.sax.SAXParseException: src-resolve: cannot resolve the name "xml: lang" in the attribute declaration (n) ".

and with this new StreamSource("http://www.w3.org/XML/1998/namespace") org.xml.sax.SAXParseException: s4s-elt-character: characters without spaces are not allowed in scheme elements other than ' xs: appinfo 'and' xs: documentation '.. Saw' The "xml:" Namespace.

Any help would be greatly appreciated.

+4
source share
2 answers

The XML schema for the namespace http://www.w3.org/XML/1998/namespace is here: http://www.w3.org/2001/xml.xsd

So, you can simply 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="http://www.w3.org/2001/xml.xsd"/> ... 

This will work, but note that W3C does not like the huge traffic for this file: http://www.w3.org/2001/xml.xsd . Thus, they artificially delay access to it.

Many programs contain local copies of such schemes. (This is why the location of the schema is not indicated. Schema software usually downloads it from its resources.)

You can also copy it to your computer and specify the URL of this copy.

An alternative way is to use an XML catalog, for example 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 somehow transfer this directory file to your schema software (if it supports XML directories)

+5
source

Just remove:

 <!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd"> 

from xml.xsd

And change

 <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/> 

to

If the same error, it can be useful.

-2
source

Source: https://habr.com/ru/post/1499740/


All Articles