Pregunta

I'm trying to validate an Amazon XML Feed request using the amzn-envelope.xsd, but am hitting a problem with schema, because amzn-envelope.xsd contains multiple references to amzn-base.xsd via included schemas.

Here is my example XML Feed:

<AmazonEnvelope 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
    <DocumentVersion>1.01</DocumentVersion>
    <MerchantIdentifier>A38Z13EKY7MB4Y</MerchantIdentifier>
</Header>
<MessageType>OrderFulfillment</MessageType>
<Message>
    <MessageID>1</MessageID>
    <OrderFulfillment>
        <AmazonOrderID></AmazonOrderID>
        <MerchantFulfillmentID>2148031177</MerchantFulfillmentID>
        <FulfillmentDate>2013-06-25T09:54:22Z</FulfillmentDate>
        <FulfillmentData>
            <CarrierName>UKMail Business Class</CarrierName>
            <ShippingMethod>CU - Next Day</ShippingMethod>
            <ShipperTrackingNumber>30995140015293</ShipperTrackingNumber>
        </FulfillmentData>
    </OrderFulfillment>
</Message>
</AmazonEnvelope>

When using the code like below to validate:

SchemaAmazonEnvelopeURL = "https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/amzn-envelope.xsd";
XDocument xdoc = XDocument.Parse(xml_data);
bool success = false;
try 
{
    success = Validate(xdoc, SchemaAmazonEnvelopeURL);
} 
catch (Exception ex)
{
    log.Error("Failed to validate against AWS Schema\n\n" + ex.Message);
}

Assert.IsTrue(success, "Failed validation");
public bool Validate(XDocument xDocument, string xsdSchema)
{
    bool success = true;
    XmlSchemaSet schemas = new XmlSchemaSet();
    schemas.Add("", xsdSchema);

    // Validate
    xDocument.Validate(schemas, (o, e) =>
    {
        throw e.Exception;
    });

    return success;
}

I get the following error:

The complexType 'NoiseLevelDimension' has already been declared.

From looking at the schema I can see that amzn-base.xsd is included more than once via other included schemas which is why the validation is failing.

How can I get around this problem?

¿Fue útil?

Solución

[I've edited the question to remove the claim that there are circular references in the schema; examination of the schema documents shows that there are no circular references, only multiple inclusions of the same schema document.]

You've run into one of the dark places in the XSD specification. Section 4.2.1 of v1.0 of that spec has a note reading

Note: The above is carefully worded so that multiple <include>ing of the same schema document will not constitute a violation of clause 2 of Schema Properties Correct (§3.15.6), but applications are allowed, indeed encouraged, to avoid <include>ing the same schema document more than once to forestall the necessity of establishing identity component by component.

This establishes pretty clearly (a) that it would be conforming behavior for your schema validator to notice that it has already include amz-base.xsd, when it sees the second inclusion (and similarly for the other multiply included schema documents) and carry on -- presumably the processors Amazon used to test its schema did this -- but also (b) that this behavior is "allowed, indeed encouraged", and (one can only infer) not required. That is, I think it's impossible to show that the behavior of your processor is non-conforming.

I do not know a good work-around; one non-good work-around would be to create a schema document of your own for the schema, by making local copies of all the schema documents in question and commenting out the inclusions in all but the top-level amzn-envelope.xsd, and use that local copy when working with processors that reject the copy of the schema documents on ssl-images-amazon.com. Good luck.

You might try asking your vendor whether there is a way to change the behavior of the schema processor in this case.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top