How to Validate an Xml File Against an Xsd File

How to validate an XML file against an XSD file?

The Java runtime library supports validation. Last time I checked this was the Apache Xerces parser under the covers. You should probably use a javax.xml.validation.Validator.

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.net.URL;
import org.xml.sax.SAXException;
//import java.io.File; // if you use File
import java.io.IOException;
...
URL schemaFile = new URL("http://host:port/filename.xsd");
// webapp example xsd:
// URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
// local file example:
// File schemaFile = new File("/location/to/localfile.xsd"); // etc.
Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {}

The schema factory constant is the string http://www.w3.org/2001/XMLSchema which defines XSDs. The above code validates a WAR deployment descriptor against the URL http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd but you could just as easily validate against a local file.

You should not use the DOMParser to validate a document (unless your goal is to create a document object model anyway). This will start creating DOM objects as it parses the document - wasteful if you aren't going to use them.

unable to validate xml against xsd

Add the attribute elementFormDefault="qualified" to the root element of your schema.

The default is "unqualified", which can't be used when specifying a default namespace.

Alternatively, you could leave your schema as is, and update your document to use the unqualified format (no default namespace, and only qualify the global elements):

<st:student xmlns:st="http://www.edureka.co/Student"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.edureka.co/Student studentRule.xsd"
>

<id>1234</id>
<name>Pradeep</name>
<language>Sanskrit</language>
<expertise>Beginner</expertise>
</st:student>

The xml document you posted is in "qualified" format; every element is explicitly associated with a namespace, which you did by specifying a default - xmlns="http://www.edureka.co/Student".

The xml above is in "unqualified" format; the global elements (i.e. the top level elements in the schema) have to be qualified, but the local elements (i.e. the nested elements in the schema) must be unqualified. In "unqualified" format the local elements are implicitly associated with the namespace of the enclosing global element.

A default namespace can't be used in documents that use "unqualified" because it explicitly associates elements without a namespace prefix with a namespace. Think of it as associating "the empty prefix" with a namespace.

In general, the "qualified" format is the easiest one to use.

No solution to validate XML against XSD in Android

1 - Go to https://code.google.com/archive/p/xerces-for-android/

2 - Click on source in left menu & in source in the middle page.

3 - Click on download the code & extract the zip contents.

4 - In the extracted content navigate to the folder "xerces-for-android/trunk/src".

5 - Copy the folder "mf" in your [AndroidApp|AndroidModule] "src/main/java" directory.

6 - Now you have to create a folder called "resources" on your "src/main" directory. This can be done in Android Studio (RightClick on your Module/App -> New -> Folder -> Java Resource Folder).

7 - Inside that folder you must create the next folder structure:

mf/org/apache/xerces/impl/msg

&

mf/org/apache/xerces/impl/xpath/regex

8 - Copy the files with ".properties" extension from the equivalent extranted contents folders inside this directories.


When you validate the XML files with XSD with something like that

SchemaFactory factory = new XMLSchemaFactory();
Source schemaFile = new StreamSource( /*Input stream to your schema*/ );
Source xmlSource = new StreamSource( /*Input stream to your xml source*/ );
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlSource);

Don't use the SDK imports and use the mf... package ones.

import mf.javax.xml.transform.Source;
import mf.javax.xml.transform.stream.StreamSource;
import mf.javax.xml.validation.Schema;
import mf.javax.xml.validation.SchemaFactory;
import mf.javax.xml.validation.Validator;
import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory;

Edited to reply the comment.

When I was looking for information on how to validate Xml documents with Xml Schema in Android I stumbled upon comments about this project, although it only contained a zip and no documentation.

I looked for a repository that had commits and found this https://github.com/MaTriXy/xerces-for-android where the last commit was made 4 years ago.

After copying the .java files but not the .profile within resources, an exception was thrown, saying that the .profile were not found, look for new information and I saw that the .profile have to be inside the resources folder in Android Studio.

Probably, and this is an assumption, the project was a jar for Eclipse/Ant and now does not work with AndroidStudio/Gradle.

You can create an .aar. Create an Android library module in your project, perform the same steps and compile the application for release.

Within your project, in the directory.

/Yourproject/yourmodule/build/outputs/aar/

There will be a file called yourmodule-release.arr and you should be able to use that package and delete the module.

Looking for information I also found information about jarjar which seems to be a project to make this adaptation in a simpler way.

https://github.com/shevek/jarjar

Have a nice day.

Validate only a part of XML file using an XSD file

There are two ways (at least) to achieve this, in principle.

First, you can in principle tell the validator which elements in the document you want to validate; the XSD spec does not require validation to start at the document root. In practice, command-line validators almost never provide run-time options for starting validation anywhere but the root. I think validation libraries are more likely to provide that functionality; they often (or at least sometimes) provide functions to allow you to pass in the element at which validation should start, together with the necessary schema information.

If your validator doesn't allow you to validate selectively, you can write a schema that contains declarations for just those elements and attributes you want to validate, and invoke a validator on the document root in "lax validation mode" -- which means, essentially "If you find in the schema a declaration for an element in the document, then validate the element against its declaration, otherwise accept it (pretend it matches a lax wildcard in the declaration of its parent) and move on." The validator will thus ignore elements for which you provide no declarations and validate elements for which you do provide declarations. (Note that conforming XSD processors are not required to provide lax-validation mode, and the definition of lax validation in the spec is a little underspecified, but I believe most available processors do support it and do the same thing in lax mode.)

Validate an XML using XSLT/XSD

That's certainly possible if you use a schema-aware XSLT processor to initiate the validation; though it's complicated by the fact that a single transformation can only load one schema (this could be the union of the two schemas if they are disjoint, eg. in different namespaces; but it would be messy if they are different versions of the same schema.

A better solution might be to implement this as an XProc pipeline. Or there are plenty of other technologies you could use depending on what you're comfortable with, for example Ant or Gradle.

How to validate an xml file against an XSD Schema using Amara library in Python?

If you're open to using another library besides amara, try lxml. It supports what you're trying to do pretty easily:

from lxml import etree

source_file = 'test.xml'
schema_file = 'test.xsd'

with open(schema_file) as f_schema:

schema_doc = etree.parse(f_schema)
schema = etree.XMLSchema(schema_doc)
parser = etree.XMLParser(schema = schema)

with open(source_file) as f_source:
try:
doc = etree.parse(f_source, parser)
except etree.XMLSyntaxError as e:
# this exception is thrown on schema validation error
print e


Related Topics



Leave a reply



Submit