2

For example, I have 2 Xml schemas:

a.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="test" targetNamespace="test">
    <xsd:include schemaLocation="b.xsd" />
</xsd:schema>`

b.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:simpleType name="testType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="test"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="test" type="testType"/>
</xsd:schema>

Second schema does not have targetNamespace and used as a chameleon schema.

I am trying to pre-load these schemas using XmlSchemaSet:

XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, @"a.xsd");

foreach (XmlSchema schema in schemaSet.Schemas())  // foreach is used to simplify the example
{
    Console.WriteLine("Target namespace: "schema.TargetNamespace);  // "Target namespace: test"
    XmlSchemaInclude include = (XmlSchemaInclude)schema.Includes[0];
    Console.WriteLine("Include target namespace: " + include.Schema.TargetNamespace); // "Include target namespace: test"
}

But after I do it both schemas have "test" target namespace. I expect instantiated schema objects should be equal to source schemas, but it is not true for schema "b.xsd". Why it behaves so and is there any way to disable such a behavior?

alexanderb
  • 253
  • 2
  • 6

1 Answers1

2

When you include b.xsd from a.xsd, you are effectively saying that you want b.xsd to have the same target namespace as a.xsd. The term chameleon include denotes the process of making that happen, given a schema document which (like b.xsd) has no specification of a target namespace.

If you want the test element and the testType type declared in b.xsd not to be in namespace test, then you do not want to use b.xsd as a chameleon, and you should not include b.xsd from a.xsd. You may wish to use xs:import instead.

But perhaps I don't understand what you are after.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65