2

例如,我有 2 个 Xml 模式:

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>

第二个模式没有targetNamespace并用作变色龙模式。

我正在尝试使用 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"
}

但是在我这样做之后,两个模式都有“测试”目标命名空间。我希望实例化的模式对象应该等于源模式,但模式“b.xsd”并非如此。为什么它的行为如此,有什么方法可以禁用这种行为?

4

1 回答 1

2

当您从 a.xsd 中包含 b.xsd 时,您实际上是在说您希望 b.xsd 具有与 a.xsd 相同的目标命名空间。术语chameleon include表示实现这一点的过程,给定一个模式文档(如 b.xsd)没有目标命名空间的规范。

如果您希望 b.xsd 中声明的test元素和testType类型不在 namespace 中test,那么您不希望将 b.xsd 用作变色龙,并且不应将 b.xsd 包含在 a.xsd 中。您可能希望改用 xs:import。

但也许我不明白你在追求什么。

于 2013-02-12T19:11:52.223 回答