Xml Validation Using Xsd Schema

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 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.

Xml validation using XSD schema

Would not this do what you are after ?

Create an XmlReaderSettings object and enable warnings through that object.

Unfortunately, there seems to be no way to pass your own XmlReaderSettings object to XmlDocument.Validate().

Instead, you can use a validating XmlReader and an XmlNodeReader to validate an existing XmlDocument (using a XmlNodeReader with StringReader rather than an XmlDocument)

XmlDocument x = new XmlDocument();
x.LoadXml(XmlSource);

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += Handler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, ExtendedTreeViewSchema);
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation ;

StringReader r = new StringReader(XmlSource);

using (XmlReader validatingReader = XmlReader.Create(r, settings)) {
while (validatingReader.Read()) { /* just loop through document */ }
}

And the handler:

private static void Handler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
System.Diagnostics.Trace.WriteLine(
String.Format("Line: {0}, Position: {1} \"{2}\"",
e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message));
}

Using XSD to validate one element, or other element, or both

Yes, this XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Message">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="Edit"/>
<xs:element name="Delete" minOccurs="0"/>
</xs:sequence>
<xs:element name="Delete"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

will allow <Edit/> or <Delete/> or <Edit/><Delete/>, as requested.

XML validation Using XSD Not Working in C#

Your XML file is missing the schema namespace. Change it to

<TXLife xmlns="http://tempuri.org/NewApplicationSchema.xsd">
...
</TXLife>

XML validation against XSD with javax.xml.validation.Validator: cannot resolve types come from 2nd XSD

I suspect that when the schema document is loaded using getResourceAsStream("schema/my.xsd"); it doesn't have a known base URI and therefore the relative URI xmldsig-core-schema.xsd doesn't resolve properly. Try setting a base URI (systemId property) on your StreamSource object.

XML validation in Azure Data Factory

I think it says, we should reference the XSD file(use relative path) in the source xml file.

For example:

In the ADF I set a Copy activity:
Sample Image
Here my xml file and xsd file are in the same folder and as follow:

Sample Image

In the order.xml, use xsi:noNamespaceSchemaLocation="order.xsd" to specify the xsd file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>  
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="order.xsd">
<orderItem>43546533</orderItem>
<orderItem>53546533</orderItem>
<orderItem>73546533</orderItem>
<orderItem>93546533</orderItem>
<orderItem>43546533</orderItem>
<orderItem>73546533</orderItem>
<orderItem>03546533</orderItem>
<orderItem>33546533</orderItem>
<orderItem>43216533</orderItem>
<orderItem>1246533</orderItem>
<orderItem>4466533</orderItem>
</order>

order.xsd:

<?xml version="1.0" encoding="UTF-8"?>  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="orderItem" type="xsd:string" maxOccurs="10"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

In my case, when the order.xml doesn't meet my verification rules, it will fails:
Sample Image

Hope my answer is helpful to you!



Related Topics



Leave a reply



Submit