例如,我有 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”来说并非如此。 为什么它会这样,有什么方法可以禁用这种行为吗?
最佳答案
当您从 a.xsd 中包含 b.xsd 时,实际上是在表示您希望 b.xsd 具有与 a.xsd 相同的目标命名空间。术语“chameleon include”表示在给定一个没有目标命名空间规范的模式文档(如 b.xsd)的情况下实现这一点的过程。
如果您希望 b.xsd 中声明的 test
元素和 testType
类型不在命名空间 test
中,那么您不需要想要将 b.xsd 用作变色龙,并且不应包含 a.xsd 中的 b.xsd。您可能希望改用 xs:import。
但也许我不明白你在追求什么。
关于.net - 为什么 XmlSchemaSet 显式将 targetNamespace 设置为变色龙架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839726/