Вопрос

I have an xml query which should be xsd validated ...(impossible ??)

Unfortunately the xsd schema for the xml contains an xsd:choice between two sequences.

but i am specifying both these sequences in my xml query.

this is causing the xsd to fail the xml, any direction as to how i override this check ?

Provided:

1. The XSD schema is standardized & cannot be altered.

2. Any change must be done at the C# code behind where i am applying the validation.

the same has been asked in :

Ignore element order while validating XML against XSD

but the answer there was to modify the xsd schema, which was the accepted answer by the user.

update1

the xml is similar to this :

...
<elem>
    <subElemA>textValA</subElemA>
    <subElemB>textValA</subElemB>
<elem>
...

but the XSD says :

...
<xsd:choice>
    <xsd:element name="subElemA" minOccurs="0" maxOccurs="1">
    </xsd:element>
    <xsd:element name="subElemB" minOccurs="0" maxOccurs="1">
    </xsd:element>
</xsd:choice>
...

& if i were to remove either subElemA OR subElemB from the xml, then the xml will schema validate successfully.

update2

i am hoping to find a solution which involves tweaking my schema validation code to allow the schema validator to bypass this check; Code for schema validation :

   XmlReaderSettings setting = new XmlReaderSettings();
   setting.ConformanceLevel = ConformanceLevel.Document;
   setting.IgnoreComments = true;
   setting.IgnoreWhitespace = true;
   setting.IgnoreProcessingInstructions = true;
   setting.ValidationType = ValidationType.Schema;
   setting.Schemas.Add(defaultNameSpace, schemaLocation.AbsoluteUri);
   setting.ValidationEventHandler += new ValidationEventHandler(this.SchemaValidationCallBack);
   MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xmlToValidate));
   XmlReader reader = XmlReader.Create(ms, setting);
   while (reader.Read());
Это было полезно?

Решение

It's likely that what you're attempting to do isn't possible. From the validator's perspective, the XML schema is the contract that you're attempting to verify. If your document is inconsistent with that contract, then the validator can't accept it (otherwise it would be going against what you've asked it to do).

If you really need to accept this XML document, the way you would instruct the validator to accept it would be to change the contract its trying to enforce (the schema). The best way to accomplish this would be to work with your third party to help them resolve conflicts between their schema and the instance documents they're providing. Without doing this, anyone else attempting to fulfill the contract is going to run into the same problems you are.

If that's not possible, you could circumvent the system by creating a local schema file that has been modified to allow the instance document to be validated. Instead of loading the customers schema into your validator, load your alternate implementation instead. This would allow you to validate the document without the third party updating the official schema. This can easily turn into a maintenance issue as your local copy may become out-of-sync with changes in the original schema, however if they're unwilling to change the schema for this instance document, it is possible the schema isn't changing frequently anyway.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top