Skip to content

Instantly share code, notes, and snippets.

@raphw
Last active August 14, 2021 20:56
Show Gist options
  • Save raphw/3e8be6dc634649cdeb0d44fd9eccd52e to your computer and use it in GitHub Desktop.
Save raphw/3e8be6dc634649cdeb0d44fd9eccd52e to your computer and use it in GitHub Desktop.
Sample for use of schemata without location specification.
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="A"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Line1" type="xs:string" />
<xs:element name="Line2" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="B"
xmlns:a="A"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:import namespace="A" />
<xs:complexType name="CustomerType">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="address" type="a:AddressType" />
</xs:sequence>
</xs:complexType>
</xs:schema>
import org.w3c.dom.ls.LSInput;
import javax.xml.XMLConstants;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
public class XsdSample {
public static void main(String[] args) throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setResourceResolver((type, namespaceURI, publicId, systemId, baseURI) -> {
if (namespaceURI.equals("A")) {
LSInput input = new SimpleLSInput();
try {
input.setByteStream(XsdSample.class.getResource("/a.xsd").openStream());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return input;
}
return null;
});
Schema schema = factory.newSchema(XsdSample.class.getResource("/b.xsd"));
}
private static class SimpleLSInput implements LSInput {
private Reader characterStream;
private InputStream byteStream;
private String stringData;
private String systemId, publicId, baseURI, encoding;
private boolean certifiedText;
@Override
public Reader getCharacterStream() {
return characterStream;
}
@Override
public void setCharacterStream(Reader characterStream) {
this.characterStream = characterStream;
}
@Override
public InputStream getByteStream() {
return byteStream;
}
@Override
public void setByteStream(InputStream byteStream) {
this.byteStream = byteStream;
}
@Override
public String getStringData() {
return stringData;
}
@Override
public void setStringData(String stringData) {
this.stringData = stringData;
}
@Override
public String getSystemId() {
return systemId;
}
@Override
public void setSystemId(String systemId) {
this.systemId = systemId;
}
@Override
public String getPublicId() {
return publicId;
}
@Override
public void setPublicId(String publicId) {
this.publicId = publicId;
}
@Override
public String getBaseURI() {
return baseURI;
}
@Override
public void setBaseURI(String baseURI) {
this.baseURI = baseURI;
}
@Override
public String getEncoding() {
return encoding;
}
@Override
public void setEncoding(String encoding) {
this.encoding = encoding;
}
@Override
public boolean getCertifiedText() {
return certifiedText;
}
@Override
public void setCertifiedText(boolean certifiedText) {
this.certifiedText = certifiedText;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment