Sumthg
Sumthg

Reputation: 113

XSD 1.1 validation for both Java and .NET C#?

I have an XSD with schema version 1.1, and I want to validate an XML against it programmatically via .NET and Java.

In .NET I use XmlSchemaSet class to validate XML against XSD, but it throws the following exception:

`System.Xml.Schema.XmlSchemaException: The 'http://www.w3.org/2001/XMLSchema:assert' element is not supported in this context.

So I guess .NET still supports XSD 1.0 and doesn't support XSD 1.1 ?

Upvotes: 5

Views: 3112

Answers (2)

patrick fogarty
patrick fogarty

Reputation: 61

I am late to this question because it has taken me years to find a solution for .Net that didn't cost anything, which was a condition for me though not for this question. The link provided by kjhughes covers the first half of the question, performing validation in Java.

Below I am using the excellent IKVM project to reference xerces2-j directly to handle the .Net side of the question. I am using the download, Xerces-J-bin.2.12.2-xml-schema-1.1.zip.

The solution requires four jar files from xerces, which are kept in the subfolder named 'include' in this project folder.

  1. cupv10k-runtime.jar
  2. org.eclipse.wst.xml.xpath2.processor_1.2.1.jar
  3. xercesImpl.jar
  4. xml-apis.jar

Then this must be added to the project file for IKVM to do its thing.

<ItemGroup>
  <PackageReference Include="IKVM" Version="8.7.1" />
</ItemGroup>

<ItemGroup>
    <IkvmReference Include="include\org.eclipse.wst.xml.xpath2.processor_1.2.1.jar">
        <AssemblyName>eclipse-xpath2</AssemblyName>
        <AssemblyVersion>1.2.1.0</AssemblyVersion>
        <AssemblyFileVersion>1.2.1.0</AssemblyFileVersion>
    </IkvmReference>
    <IkvmReference Include="include\cupv10k-runtime.jar">
        <AssemblyName>cup-runtime</AssemblyName>
        <AssemblyVersion>0.10.0</AssemblyVersion>
        <AssemblyFileVersion>0.10.0</AssemblyFileVersion>
    </IkvmReference>
    <IkvmReference Include="include\xml-apis.jar">
        <AssemblyName>xml-apis</AssemblyName>
        <AssemblyVersion>1.7.0</AssemblyVersion>
        <AssemblyFileVersion>1.7.0</AssemblyFileVersion>
    </IkvmReference>
    <IkvmReference Include="include\xercesImpl.jar">
        <AssemblyName>xerces</AssemblyName>
        <AssemblyVersion>2.12.2.0</AssemblyVersion>
        <AssemblyFileVersion>2.12.2.0</AssemblyFileVersion>
        <References>include/cupv10k-runtime.jar;include/org.eclipse.wst.xml.xpath2.processor_1.2.1.jar;include/xml-apis.jar</References>
    </IkvmReference>
</ItemGroup>

Then this simple program.cs will validate xml properly.

using javax.xml.transform;
using javax.xml.transform.stream;
using jio = java.io;
using javax.xml.validation;
using org.apache.xerces.impl;
using org.xml.sax;
using System.Reflection;
internal class Program
{
    static void Main(string[] args)
    {
        ikvm.runtime.Startup.addBootClassPathAssembly(Assembly.Load("eclipse-xpath2"));
        ikvm.runtime.Startup.addBootClassPathAssembly(Assembly.Load("cup-runtime"));
        ikvm.runtime.Startup.addBootClassPathAssembly(Assembly.Load("xml-apis"));
        ikvm.runtime.Startup.addBootClassPathAssembly(Assembly.Load("xerces"));
        
        jio.File schemaLocation = new jio.File("/path/to/some.xsd");
        jio.File xmlFile        = new jio.File("/path/to/some.xml");

        Source xsdSource = new StreamSource(schemaLocation);
        Source xmlSource = new StreamSource(xmlFile);

        SchemaFactory factory = SchemaFactory.newInstance(Constants.W3C_XML_SCHEMA11_NS_URI);
        Schema schema = factory.newSchema(xsdSource);
        Validator validator = schema.newValidator();


        try
        {
            validator.validate(xmlSource);
            Console.WriteLine(xmlFile.getName() + " is valid.");
        }
        catch (SAXException ex)
        {
            Console.WriteLine(xmlFile.getName() + " is not valid because ");
            Console.WriteLine(ex.getMessage());
        }
    }
}

Upvotes: 2

kjhughes
kjhughes

Reputation: 111491

Correct, .NET supports XSD 1.0, not XSD 1.1. You can use Xerces to validate XML against XSD 1.1 in Java, but there are no free libraries that support XSD 1.1 on .NET.

To validate XML using XSD 1.1 on .NET, you'll need a commercial license for Saxon EE (full product) or EE-V (validation package). Note that a Saxon license will allow you to use the library on both .NET and Java platforms. See Saxonica's product page, feature matrix, and licensing for further details.

Upvotes: 8

Related Questions