How to create XML document with multiple schemas in Java? - java

I have XSD which describes custom schema and which imports XLink (another schema).
Import is made with the following declaration ix main XSD:
<xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
xlink.xsd file is actually located near the main XSD.
Then I configure builders with the following code
static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
static final String MY_SCHEMA_FILENAME = "mydir/myfile.xsd";
static final String MY_DATA_FILENAME = "mydir/myfile.xml";
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
try {
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(MY_SCHEMA_FILENAME));
}
catch (IllegalArgumentException e) {
throw new AssertionError(e);
}
try {
builder = factory.newDocumentBuilder();
}
catch(ParserConfigurationException e) {
throw new AssertionError(e);
}
when I prepare document in memory, I set attribute the following way
imageElement.setAttribute("xlink:href", mypathvariable);
I expect this will create tag which is described following way in XSD
<xs:element name="image">
<xs:complexType>
<xs:attribute ref="xlink:href" use="required"/>
</xs:complexType>
</xs:element>
While creating everything works without any errors, but while saving with code
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(MY_DATA_FILENAME));
transformer.transform(source, result);
the following error occurs:
ERROR: 'Namespace for prefix 'xlink' has not been declared.'
Where is my mistake?

Use setAttributeNS instead, something like this:
imageElement.setAttributeNS("http://www.w3.org/1999/xlink", "href", mypathvariable);
If you want to stick with:
imageElement.setAttribute("xlink:href", mypathvariable);
Then make sure you have this defined (typically on the root element), on some element that provides scope to where your attribute is being added:
someElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
The above also shows how to control a prefix in general.

Related

Java Validate XML Schema with XSDs that import each other [duplicate]

I'm trying to validate an XML file against a number of different schemas (apologies for the contrived example):
a.xsd
b.xsd
c.xsd
c.xsd in particular imports b.xsd and b.xsd imports a.xsd, using:
<xs:include schemaLocation="b.xsd"/>
I'm trying to do this via Xerces in the following manner:
XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory();
Schema schema = xmlSchemaFactory.newSchema(new StreamSource[] { new StreamSource(this.getClass().getResourceAsStream("a.xsd"), "a.xsd"),
new StreamSource(this.getClass().getResourceAsStream("b.xsd"), "b.xsd"),
new StreamSource(this.getClass().getResourceAsStream("c.xsd"), "c.xsd")});
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlContent)));
but this is failing to import all three of the schemas correctly resulting in cannot resolve the name 'blah' to a(n) 'group' component.
I've validated this successfully using Python, but having real problems with Java 6.0 and Xerces 2.8.1. Can anybody suggest what's going wrong here, or an easier approach to validate my XML documents?
So just in case anybody else runs into the same issue here, I needed to load a parent schema (and implicit child schemas) from a unit test - as a resource - to validate an XML String. I used the Xerces XMLSchemFactory to do this along with the Java 6 validator.
In order to load the child schema's correctly via an include I had to write a custom resource resolver. Code can be found here:
https://code.google.com/p/xmlsanity/source/browse/src/com/arc90/xmlsanity/validation/ResourceResolver.java
To use the resolver specify it on the schema factory:
xmlSchemaFactory.setResourceResolver(new ResourceResolver());
and it will use it to resolve your resources via the classpath (in my case from src/main/resources). Any comments are welcome on this...
http://www.kdgregory.com/index.php?page=xml.parsing
section 'Multiple schemas for a single document'
My solution based on that document:
URL xsdUrlA = this.getClass().getResource("a.xsd");
URL xsdUrlB = this.getClass().getResource("b.xsd");
URL xsdUrlC = this.getClass().getResource("c.xsd");
SchemaFactory schemaFactory = schemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//---
String W3C_XSD_TOP_ELEMENT =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
+ "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">\n"
+ "<xs:include schemaLocation=\"" +xsdUrlA.getPath() +"\"/>\n"
+ "<xs:include schemaLocation=\"" +xsdUrlB.getPath() +"\"/>\n"
+ "<xs:include schemaLocation=\"" +xsdUrlC.getPath() +"\"/>\n"
+"</xs:schema>";
Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(W3C_XSD_TOP_ELEMENT), "xsdTop"));
The schema stuff in Xerces is (a) very, very pedantic, and (b) gives utterly useless error messages when it doesn't like what it finds. It's a frustrating combination.
The schema stuff in python may be a lot more forgiving, and was letting small errors in the schema go past unreported.
Now if, as you say, c.xsd includes b.xsd, and b.xsd includes a.xsd, then there's no need to load all three into the schema factory. Not only is it unnecessary, it will likely confuse Xerces and result in errors, so this may be your problem. Just pass c.xsd to the factory, and let it resolve b.xsd and a.xsd itself, which it should do relative to c.xsd.
From the xerces documentation :
http://xerces.apache.org/xerces2-j/faq-xs.html
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
...
StreamSource[] schemaDocuments = /* created by your application */;
Source instanceDocument = /* created by your application */;
SchemaFactory sf = SchemaFactory.newInstance(
"http://www.w3.org/XML/XMLSchema/v1.1");
Schema s = sf.newSchema(schemaDocuments);
Validator v = s.newValidator();
v.validate(instanceDocument);
I faced the same problem and after investigating found this solution. It works for me.
Enum to setup the different XSDs:
public enum XsdFile {
// #formatter:off
A("a.xsd"),
B("b.xsd"),
C("c.xsd");
// #formatter:on
private final String value;
private XsdFile(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
Method to validate:
public static void validateXmlAgainstManyXsds() {
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
String xmlFile;
xmlFile = "example.xml";
// Use of Enum class in order to get the different XSDs
Source[] sources = new Source[XsdFile.class.getEnumConstants().length];
for (XsdFile xsdFile : XsdFile.class.getEnumConstants()) {
sources[xsdFile.ordinal()] = new StreamSource(xsdFile.getValue());
}
try {
final Schema schema = schemaFactory.newSchema(sources);
final Validator validator = schema.newValidator();
System.out.println("Validating " + xmlFile + " against XSDs " + Arrays.toString(sources));
validator.validate(new StreamSource(new File(xmlFile)));
} catch (Exception exception) {
System.out.println("ERROR: Unable to validate " + xmlFile + " against XSDs " + Arrays.toString(sources)
+ " - " + exception);
}
System.out.println("Validation process completed.");
}
I ended up using this:
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
.
.
.
try {
SAXParser parser = new SAXParser();
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "http://your_url_schema_location");
Validator handler = new Validator();
parser.setErrorHandler(handler);
parser.parse("file:///" + "/home/user/myfile.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException ex) {
e.printStackTrace();
}
class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception)
throws SAXException {
}
}
Remember to change:
1) The parameter "http://your_url_schema_location" for you xsd file location.
2) The string "/home/user/myfile.xml" for the one pointing to your xml file.
I didn't have to set the variable: -Djavax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
Just in case, anybody still come here to find the solution for validating xml or object against multiple XSDs, I am mentioning it here
//Using **URL** is the most important here. With URL, the relative paths are resolved for include, import inside the xsd file. Just get the parent level xsd here (not all included xsds).
URL xsdUrl = getClass().getClassLoader().getResource("my/parent/schema.xsd");
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(xsdUrl);
JAXBContext jaxbContext = JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
/* If you need to validate object against xsd, uncomment this
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<MyClass> wrappedObject = objectFactory.createMyClassObject(myClassObject);
marshaller.marshal(wrappedShipmentMessage, new DefaultHandler());
*/
unmarshaller.unmarshal(getClass().getClassLoader().getResource("your/xml/file.xml"));
If all XSDs belong to the same namespace then create a new XSD and import other XSDs into it. Then in java create schema with the new XSD.
Schema schema = xmlSchemaFactory.newSchema(
new StreamSource(this.getClass().getResourceAsStream("/path/to/all_in_one.xsd"));
all_in_one.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ex="http://example.org/schema/"
targetNamespace="http://example.org/schema/"
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="relative/path/to/a.xsd"></xs:include>
<xs:include schemaLocation="relative/path/to/b.xsd"></xs:include>
<xs:include schemaLocation="relative/path/to/c.xsd"></xs:include>
</xs:schema>

Java: string xml validation against xsd that include another xsd

Update 1: New Validation method throws new error, see below.
Update 2: i have checked the xml files with xml spy. No errors there.
Hope somebody see where iam wrong.
i'm currently trying to validate xml file against an xsd which includes another xsd. I don' know if my xsds or the java code is faulty. I dont have the xml/xsd files stored, i get them as a base64 string from the server. Hope somebody can help.
I get the fllowing error:
org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 104; src-resolve: Cannot resolve the name 'ServiceSpecificationSchema:ServiceIdentifier' to a(n) 'type definition' component.
-ServiceSpecificationSchema.xsd
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example.org/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN">
<include schemaLocation="ServiceBaseTypesSchema.xsd"/>
<element name="serviceSpecification" type="ServiceSpecificationSchema:ServiceSpecification">
<unique name="serviceDataModelTypeKey">
<selector xpath=".//xs:*"/>
<field xpath="#name"/>
</unique>
<keyref name="serviceDataModelReturnValueTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey">
<selector xpath=".//ServiceSpecificationSchema:returnValueType"/>
<field xpath="ServiceSpecificationSchema:typeReference"/>
</keyref>
<keyref name="serviceDataModelParameterTypeTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey">
<selector xpath=".//ServiceSpecificationSchema:parameterType"/>
<field xpath="ServiceSpecificationSchema:typeReference"/>
</keyref>
</element>
<complexType name="ServiceSpecification">
<all>
<element name="id" type="ServiceSpecificationSchema:ServiceIdentifier" minOccurs="1" maxOccurs="1"/>
.....
ServiceBaseTypeSchema:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN">
...
<simpleType name="ServiceIdentifier">
<restriction base="string"/>
</simpleType>
...
</schema>
Java Validator
public void validate(String inputXml, String XSD, String XSD2)
throws SAXException, IOException {
try {
byte[] decoded = Base64.decodeBase64(XSD);
XSD = new String(decoded, "UTF-8");
byte[] decoded2 = Base64.decodeBase64(XSD2);
XSD2 = new String(decoded2, "UTF-8");
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(new SAXSource[]
{
(new SAXSource(new InputSource(new StringReader(XSD)))),
(new SAXSource(new InputSource(new StringReader(XSD2))))
});
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(inputXml)));
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
I found another possible solution, but it still don't work. With the following method, the exeption is thrown, wenn validating the xsds against the xml. The line of the excetion depends on the used xml. In the Example on the bottom, the error is in line 2. -> Error: Cannot find the declaration of element 'ServiceSpecificationSchema:serviceSpecification'
I think: 1. My java code is faulty or 2. all the xml files are.
New Java Validator:
byte[] decoded = Base64.decodeBase64(XSD);
byte[] decoded2 = Base64.decodeBase64(XSD2);
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
InputStream impFis = new ByteArrayInputStream(decoded2);
InputStream mainFis = new ByteArrayInputStream(decoded);
Source main = new StreamSource(mainFis);
Source imp = new StreamSource(impFis);
Source[] schemaFiles = new Source[] {imp, main};
Schema schema = factory.newSchema(schemaFiles);
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(inputXml)));
Example XML file:
<?xml version="1.0" encoding="UTF-8"?>
<ServiceSpecificationSchema:serviceSpecification
xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/ServiceSpecificationSchema.xsd">
.....
Finally i found a solution. Thestandard method does not resolve included or imported files from the start. To resolve the included files i used the LSResourceResolver.
This works for me:
#Service
public class ResourceResolverImpl implements LSResourceResolver {
private ILoadFromSRService iLoadFromSRService;
#Autowired
public ResourceResolverImpl(ILoadFromSRService iLoadFromSRService){
this.iLoadFromSRService = iLoadFromSRService;
}
public LSInput resolveResource(String type,
String namespaceURI,
String publicId,
String systemId,
String baseURI) {
String string =iLoadFromSRService.getServiceBaseTypeSchema();
string = string.replace("\n", "").replace("\t", "");
InputStream resourceAsStream = new ByteArrayInputStream( string.getBytes());
return new LSInputImpl(publicId, systemId, resourceAsStream);
}
}

Glassfish using Saxon instead of Xalan, XSLT 2.0, Java

I have a problem with transforming a XML (TEI P5) to PDF. My Stylesheet is from TEI https://github.com/TEIC/Stylesheets and I call the file fo/fo.xsl. When I transform my XML using Oxygen and Saxon there is no Problem, the PDF will be generated. But in my J2EE application the transform does not work. I think because of the XSLT 2.0. So i wanted to change to an other parser. I downloaded Saxon 9HE and included it in my project. Then I experimented and changed the java call and it looks now like this:
String inputXSL2 = "/de/we/parser/xslt/fo/fo.xsl";
String inputXML2 = "/de/we/parser/testfiles/Presentation.xml";
Source xsltTarget = new StreamSource(inputXSL2);
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setURIResolver(new XsltURIResolver());
Transformer transformer = tFactory.newTransformer(xsltTarget);
Controller controller = (net.sf.saxon.Controller) transformer;
Configuration configuration = controller.getConfiguration();
configuration.setConfigurationProperty(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, Boolean.TRUE);
Source source = transformer.getURIResolver().resolve(inputXML2, "");
DocumentInfo newdoc = configuration.buildDocument(source);
configuration.getGlobalDocumentPool().add(newdoc, inputXML2);
Source streamSource = null;
StreamResult resultTarget = null;
streamSource = new StreamSource(new StringReader(inputXML2));
resultTarget = new StreamResult(new StringWriter());
transformer.transform(streamSource, resultTarget);
} catch (Exception e) {
System.out.println(e);
}
class XsltURIResolver implements URIResolver {
#Override
public Source resolve(String href, String base) throws TransformerException {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/de/we/parser/xslt/fo/" + href);
StreamSource ss = new StreamSource(inputStream);
ss.setSystemId("/de/we/parser/xslt/fo/" + href);
return ss;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
The error is:
I/O error reported by XML parser processing /de/we/parser/xslt/fo/fo.xsl:
\de\we\parser\xslt\fo\fo.xsl (Das System kann den angegebenen Pfad nicht finden)
But the file exists so i don't know whats the problem.
Had a lot other version where I tried to use Saxon, but the result was never a generated pdf :(
Need some help please
Please don't crosspost to multiple lists.
You asked the same question here
https://saxonica.plan.io/boards/3/topics/5849?r=5851#message-5851
where I answered it.

java - spring - xsl transformation dtd not found error

I have a code which transform xml file using xsl, my peace of code as following. My problem is when i run the execution point it gives me following error.
StackTrace: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: /home/app/myapp/bin/xhtml11-flat.dtd (No such file or directory)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:720)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
at com.core.util.XmlUtils.transform(XmlUtils.java:151)
at com.core.util.XmlUtils.transform(XmlUtils.java:147)
Long story short it is trying to find dtd file inside the bin directory from where i executed the application.
/home/app/myapp/bin/xhtml11-flat.dtd
I have the xhtml11-flat.dtd file if i copy this file in bin directory it works fine, instead of the bin directory i want to load it from classpath any idea how can i achieve this with minimum changes ?
I don't know from where it is laoding .dtd code so that i can set my path in it.
//Execution Point
function transform(){
Templates templates = getTemplates();
StringWriter result = new StringWriter();
XmlUtils.transform(templates.newTransformer(), input, new StreamResult(result));
...
}
private Templates getTemplates() throws Exception {
if (templates == null) {
templates = XmlUtils.createTemplates(XslRdcSourceDocTransformer.class.getResourceAsStream("/xsl/" + getXslFileName()));
}
return templates;
}
public static Templates createTemplates(InputStream stream) throws Exception {
TransformerFactory tfactory = TransformerFactory.newInstance();
return tfactory.newTemplates(new StreamSource(stream));
}
Your xml files are probably containing a doctype declaration containing a relative path to the dtd:
<!DOCTYPE html SYSTEM "xhtml11-flat.dtd">
The transformer api tries to resolve this path to the current working directory of the java program. To customize how the path is resolved you need to implement a EntityResolver. This EntityResolver can return an InputSource referring to a copy of the dtd loaded from the classpath.
public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException {
if ("xhtml11-flat.dtd".equals(systemId)) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputSource is = new InputSource();
is.setSystemId(systemId);
is.setByteStream(cl.getResourceAsStream("/com/example/dtd/xhtml11-flat.dtd"));
return is;
} else {
return null;
}
}
How you use this class depends on the type of source for your transformation. For a DOMSource you have to configure the DocumentBuilder:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
DocumentBuilder builder = ...
builder.setEntityResolver(entityResolver);
Source source = new DOMSource(builder.parse(inputStream));
For a SAXSource the setting is on the XMLReader instance:
SAXParserFactory factory1 = SAXParserFactory.newInstance();
factory1.setValidating(false);
factory1.setNamespaceAware(true);
SAXParser parser = factory1.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
xmlreader.setEntityResolver(entityResolver);
Source source = new SAXSource(xmlreader, new InputSource(stream));
The code for the transformation is the same regardless of the source type and should look similar to the code you currently have in your XmlUtils class:
Templates templates = ...
Result result = new StreamResult(...);
Transformer transformer = templates.newTransformer();
transformer.transform(source, result);

Validating XML files in Java against two schemas with same namespace

I have
an XML document,
base XSD file and
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
modifying the file itself and
merging two schemas in one file.
How can I do this in Java?
UPD: Here is the code I'm using.
SchemaFactory 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?

Categories

Resources