Project

Profile

Help

Some questions while assessing viability of Saxon for XSD 1.1 validation in .NET

Added by Lee Reynolds over 8 years ago

Hey, We have a .NET solution, which allows users to upload an XML file, which gets validated against our XSDs. It currently only supports validation against XSD 1.0, but needs updating to 1.1 support. Our existing system does the following, using .NET:

  1. Take in a Stream Input and IEnumable set of XSD objects (objects which simply specify the namespace of XSD and the URL)
  2. Attempt to load a Stream into XDocument (to determine if well-formed XML)
  3. Get Root namespace of XDocument (with Doc.Root.Name.Namespace.NamespaceName) and compare against available XSD namespaces
  4. Convert the IEnumable set of XSDs to a Schema Set
  5. Use XDocument.Validate against the Schema set

Obviously with the introduction of 1.1 XSDs, this solution won't work anymore, specifically because .NET doesn't support 1.1, so we've been looking at Saxon to determine if it can do the same.

I've done some investigations of the Saxon API and some prototyping, but was hoping for a bit of assistance before moving forward with formal licencing of the product.

  1. I'm using a DocumentBuilder, and passing in the input stream, to test if the XML (either 1.0 or 1.1) is well-formed. This seems to work, but there were 2 issues: i) Using DocumentBuilder with a Stream requires a baseURI. If I know that uploaded document won't have an relative URIs, what should I do in this case? ii) The DocumentBuilder doesn't seem to have the ErrorList which is available for SchemaManager/SchemaValidator, so it seems I need to transform the exception message into a more friendly one?

  2. Is it possible to extract the XML version and namespace from the XdmNode created with DocumentBuilder?

  3. As I need to manually set the SchemaManager.XsdVersion, how am I meant to extract this from an XSD provided via URL? In my prototype i'm using the .NET File library, passing in the SchemaUrl and reading/parsing the first line, which is not ideal if there is a more strongly-typed way


Replies (3)

Please register to reply

RE: Some questions while assessing viability of Saxon for XSD 1.1 validation in .NET - Added by Michael Kay over 8 years ago

1(i) If the base URI is never used then it doesn't matter what it is, so any URI will do.

1(ii) The DocumentBuilder is primarily for building documents as input to XSLT or XQuery, not for validation. If the primary purpose is validation, I think it would be best to create a SchemaValidator, using setDestination(new XdmDestination()) to capture the tree after validation if that's what you want to do.

2(i) Saxon doesn't know the XML version declared in the source document, in general this information isn't something that the XML parser makes available; it's certainly not part of the XDM data model, and therefore not available as a property of an XdmNode.

2(ii) Not quite sure what you mean by "the namespace", but if you mean the namespace part of the name of the outermost element, then yes, you can navigate from the XdmNode representing the document root to the XdmNode representing its first child element, and the NodeName property of that XdmNode is a QName object with a Uri property.

(3) You can't tell from looking at a schema document whether it makes use of XSD 1.1 features or not. If you don't know, I would simply set SchemaManager.XsdVersion to 1.1, since the only real effect of setting it to 1.0 is to cause 1.1 features to be rejected.

RE: Some questions while assessing viability of Saxon for XSD 1.1 validation in .NET - Added by Lee Reynolds over 8 years ago

Thanks Michael, much appreciate the prompt reply.

One other question - I'm a bit confused regarding XSD compilation - my XSD has a number of imports, for example:

<xsd:import namespace="xxx" schemaLocation="import1.xsd"/>

Currently, I'm not compiling the imports, is that correct, or should i be compiling them as well, to ensure the XML is validated against them?

Thanks in advance..

RE: Some questions while assessing viability of Saxon for XSD 1.1 validation in .NET - Added by Michael Kay over 8 years ago

When you compile a schema it will first assemble all the schema documents that are referenced and then compile them as a unit. So you don't need to do anything special. You can watch the process in action if you do it from the command line with the "-t" option.

    (1-3/3)

    Please register to reply