Validating an xml document against a local schema

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
For a project, we have to process http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
xliff documents . To make sure we can process the documents, I want to validate them against the
http://docs.oasis-open.org/xliff/v1.2/cs02/xliff-core-1.2-strict.xsd xliff schema . However, this appears to be trickier than it should be.
My first attempt was pretty straight forward:
<pre class="prettyprint XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += ValidationHandler;
settings.Schemas.Add(null, "http://docs.oasis-open.org/xliff/v1.2/cs02/xliff-core-1.2-strict.xsd");

StringReader sr = new StringReader(doc.OuterXml);
XmlReader reader = XmlReader.Create(sr, settings);

while (reader.Read()) ;[/code]
With this, the process hangs on adding the schema to the settings. A lot of research later I discovered that this is because the xliff schema imports the xml.xsd schema from w3.org; and w3.org rate throttles the access to this file. In effect, this means
we do not have access to this file.
This is no problem; it is better to validate against local files anyway! So I downloaded the xliff schema and the xml schema, and modified my code as follows:
<pre class="prettyprint XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += ValidationHandler;
settings.Schemas.Add(null, "xml.xsd");
settings.Schemas.Add(null, "xliff-core-1.2-strict.xsd");[/code]
This generates an error: parsing of DTDs is prohibited (the xml.xsd includes a DTD). So, version 3:
<pre class="prettyprint XmlReaderSettings xrs = new XmlReaderSettings();
xrs.DtdProcessing = DtdProcessing.Parse;

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += ValidationHandler;
settings.Schemas.Add(null, XmlReader.Create("xml.xsd", xrs));
settings.Schemas.Add(null, XmlReader.Create("xliff-core-1.2-strict.xsd", xrs));[/code]
Once again, the process hangs on adding the xliff schema. This schema specifies the schemaLocation of xml.xsd as " http://www.w3.org/2001/xml.xsd" rel="external" target="_blank" title="http://www.w3.org/2001/xml.xsd http://www.w3.org/2001/xml.xsd ",
and apparently .Net tries to access this location even though a local schema is available.
I removed the schemaLocation from the xliff schema, and now the process ran all the way without hanging. However, when I enabled validation warnings, I got the message: "Could not find schema information for the element xliff". A little bit of tinkering
with the xml document revealed that the document is now checked for valid xml, but not against the xliff schema.
Any suggestions on how to get this validation working?

View the full article
 
Back
Top