views:

637

answers:

1

I have problem with types in my schema when trying to use xsd:any element During validation i've got validation exception: The 'MerchantAccount' element is not declared.

The idea is to have ability to specify any properties and values within ExtendedProperties element. Please advice what am i doing wrong.

Part of the schema

...
<xsd:complexType name="ExtendedPropertiesType">
    <xsd:sequence>
      <xsd:any minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="ProcessorInstanceType">
  <xsd:all>
    <xsd:element name="Id" type="xsd:string" />
    <xsd:element name="Descriptor" type="xsd:string" />
    <xsd:element minOccurs="0" name="ExtendedProperties" type="ExtendedPropertiesType" />
  </xsd:all>
  <xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
...

Part of xml file:

...
<ProcessorInstance name="aaaa">
  <Id>37fc527b-2845-43d0-99ca-ac1ff6f0ed86</Id>
  <Descriptor>Test</Descriptor>

  <ExtendedProperties>
    <MerchantAccount>1111</MerchantAccount>
  </ExtendedProperties>
</ProcessorInstance>
...

Validation code:

private static XmlDocument loadConfigurationXml(FileInfo configFile)
    {
        //load schema
        var sr = new StringReader(Schemas.ConfigurationSchema);
        var schema = XmlSchema.Read(sr, (o, ea) => { throw ea.Exception; });
        //validate against the schema
        var schemas = new XmlSchemaSet();
        schemas.Add(schema);
        var readerSettings = new XmlReaderSettings
        {
            ValidationType = ValidationType.Schema, 
            ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
            Schemas = schemas,
        };
        readerSettings.ValidationEventHandler += (o, ea)=>
        {
            throw new PaynetValidationException(
                string.Format("Invalid configuration file, see schema for details: {0}", 
                              ea.Message), 
                ea.Exception);
        };
        var reader = XmlReader.Create(configFile.FullName, readerSettings);
        //parse and validate config file
        while (reader.Read()){}

        var ret = new XmlDocument();
        if (configFile.Length != 0)
            ret.Load(configFile.FullName);

        return ret;
    }
+1  A: 

This is because the default for the processContents attribute is strict. If you want that to validate when you don't have the schema for the elements, then use

<xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>

BTW, if you're designing this schema, I'd suggest you stay away from xs:all. It may sound like a good idea to allow elements to be entered in any order, but this can lead to ambiguous content models, and can drive the code processing the schema absolutely nuts.

John Saunders
Thanks for your response ! I've changed definition to the way you saidbut now i've got error message: 'Could not find schema information for the element 'MerchantAccount''
ILICH
But ! with processContents="skip" works perfectly, thanks for pointing the reason of error, problem solved !
ILICH
Is "could not find schema information" a warning or an error? I bet it's a warning. You can leave off `XmlSchemaValidationFlags.ReportValidationWarnings` if you don't want the warnings. Warnings are always "can't find".
John Saunders
Just to be sure you know - if you have the schemas for the elements matching the `xs:any`, then you should include them in the `XmlSchemaSet` you are using to validate. That way, you will be able to validate the contents, as well.
John Saunders