.net - 为什么 XmlSchemaSet 显式将 targetNamespace 设置为变色龙架构

标签 .net xsd system.xml xmlschemaset

例如,我有 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/

相关文章:

c# - 在 C# 中从 url(仅第一个根目录值)获取第一部分

c# - 由于 ReflectionTypeLoadException,无法从程序集中检索 TypeInfo

java - Cowardly 拒绝写入不存在的目录 - JAXB

.net - VB6 MSXML2 nodeTypedValue 的 .NET 4.6 等价物是什么?

.net - 将 XmlNode 从一个 XmlDocument 移动到另一个的直观方法是什么?

c# - 过滤后如何将位置应用于节点

c# - 除了使用 P/Invoke 之外,如何在 hBitmap 上调用 DeleteObject()?

java - schemagen.exe 不会跳过 @XmlTransient 注释类

spring - Spring 应用程序上下文模式中的错误

java - 使用 JCE 的 3DES/DES 加密 - 生成可接受的 key