7

I'm trying to do some work with C# and XML in a cross-platform app (most development is in MonoDevelop on Linux, but most users will end up using the WinForms front-end that I make in Visual Studio). Mono is behaving as I'd expect, but .Net isn't and so I'm looking for options and alternatives.

The schemas have various optional elements followed by an "xs:any" element. This works perfectly fine in Mono, but not in Microsoft's .Net as it complains that the occurrence of the optional element is ambiguous between its actual definition in the schema and "xs:any". From a bit of reading up, it appears to be a problem that was resolved in later versions of W3C's Schema definition. Obviously Mono has been kept up to date, but Microsoft are lagging.

The options I can see are:

1) Use RelaxNG - unfortunately the link from the main site to the C# implementation is broken. It is part of Mono, but that isn't much help when running on MS' .Net. The only way I can see to do it is to duplicate and rebuild Mono's version as my own DLL

2) Use Schematron - I found it as a suggestion on making backward and forward compatible XML, but it seems more like a format validator than a 'standard' schema language. I'm not quite sure where the implementation is - all I can find is a command-line validator.

3) Make sure that the MS .Net implementation uses the updated schema standard that Mono uses as well - I don't even know if this is possible.

4) Stick with ugly schemas and put "xs:any" within an optional "extensions" element - it nests it for no good reason other than the official .Net framework doesn't get confused, but it works.

The code is aimed at .Net 2.0, but I've got 3.5 installed on my machine. I'm also working with Mono 2.4, MonoDevelop 2.2 and Visual Studio Express 2005.

Are any of 1-3 possible, or am I stuck with 4?

Thanks.

IBBoard
  • 909
  • 4
  • 16
  • Please provide a link that says that the UPA rule has changed. This would be where you say the W3C has updated the schema version. – John Saunders Feb 28 '10 at 21:17
  • 1
    I think this is addressed in the latest working draft (http://www.w3.org/TR/xmlschema11-1/#sec-cos-nonambig), which does not eliminate the UPA constraint but makes a special case of how to handle a conflict between an element and a wildcard. Since this is a draft, one can't fault Microsoft. Perhaps it is not that Mono is more up to date but rather that they did not implement the UPA checking? Personally, I would not recommend designing applications that rely on a draft specification, but that is up to the OP... – Jason Kresowaty Feb 28 '10 at 21:33
  • @binarycoder: thanks. I hadn't heard of this, so was sure it wasn't part of any final spec. – John Saunders Feb 28 '10 at 22:18
  • I originally found it in an IBM article (http://www.ibm.com/developerworks/xml/library/x-xml11pt3/#N10122) and hadn't realised it was still a draft spec. It seems like a sensible enough handling anyway - if it is defined then use the defined one, else use the catch-all for extensions. I've also never seen it explained anywhere that it could be an issue, although I've seen mention of "all" being problematic and more trouble than it is worth most of the time. – IBBoard Mar 01 '10 at 20:09

2 Answers2

5

You may be able to work around the ambiguity problem by setting the XmlSchemaSet.CompilationSettings.EnableUpaCheck to false. UPA stands for the Unique Particle Attribution requirement of the XML Schema standard that you referenced.

Jason Kresowaty
  • 16,105
  • 9
  • 57
  • 84
  • 1
    In other words, the Microsoft schema validation is telling you about a problem with your schema - it's ambiguous. – John Saunders Feb 28 '10 at 21:15
  • 1
    Right, this is definitely a problem with the schema per the current standard. This is loosened in XML Schema 1.1, with the caveat that it is only of "working draft" status. – Jason Kresowaty Feb 28 '10 at 21:37
  • @binarycoder: and the point is, at Working Draft level, and since it's a different version of the standard, it's unlikely that Mono is implementing the later version of the standard and that Microsoft is behind. – John Saunders Feb 28 '10 at 22:22
  • I'll look at that and see if there are any unwanted side-effects. If there are then I guess that leaves me with 4) - adding extra tags around the xs:any just for the sake of it because some of my previous definitions at the same level are optional. Unless anyone has any options on the others. Thanks. – IBBoard Mar 01 '10 at 20:11
  • It doesn't seem to be working at the moment. I've just added: `cache.CompilationSettings.EnableUpaCheck = false;` after `XmlSchemaSet cache = new XmlSchemaSet();` but I'm still getting an XmlSchemaValidationException thrown with the message: "Wildcard '##any' allows element 'http://ibboard.co.uk/warfoundry/race:unitAbilities', and causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate....[snip]" – IBBoard Mar 05 '10 at 20:21
  • You might try asking around at http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/threads. I've had good luck getting information about intricate .NET XML issues there in the past, including from MSFT employees. – Jason Kresowaty Mar 05 '10 at 23:56
  • I've solved it now - it does work, but if you create an XmlSchemaSet and then Add() that to a XmlReaderSettings's Schemas then you need to set XmlReaderSettings.Schemas.CompilationSettings.EnableUpaCheck. Thanks for pointing me to the setting - this way makes far more sense to me. – IBBoard Mar 06 '10 at 15:55
  • *Note* that XSD 1.1 became a recommendation on 2012-04-05, so the disambiguation of an element which previously violated UPA now is assigned to the matching element. – codekaizen Mar 15 '13 at 23:27
0

I contributed to a resurrection of an ISO schematron implemented in C#. It's available on github here.

gap
  • 2,766
  • 2
  • 28
  • 37