You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不修改目标命名空间的前提下扩展命名空间中的complexType

问题:扩展已有命名空间的complexType失败

要在不修改主命名空间的前提下,为Book complexType添加自定义属性,参考相关方案操作后未成功。我创建了3个XSD文件和1个XML文件,验证时出现错误,具体如下:


original.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="http://bookshop.com" 
targetNamespace="http://bookshop.com"
elementFormDefault="qualified">

<xs:complexType name="Book">
    <xs:sequence>
        <xs:element name="Author" type="String32" 
            minOccurs="1" maxOccurs="1" />
        <xs:element name="Title" type="String32" 
            minOccurs="1" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

<xs:element name="Book" type="Book"/>

<xs:simpleType name="String32">
    <xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:schema>

added.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified"
xmlns="http://custombookshop.com" 
xmlns:bs="http://bookshop.com" 
targetNamespace="http://custombookshop.com"> 

<xs:import namespace="http://bookshop.com" schemaLocation="original.xsd"/>
<xs:element name="Publisher" type="bs:String32" />
</xs:schema>

newMainfile.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="http://bookshop.com"
targetNamespace="http://bookshop.com"
xmlns:cs="http://custombookshop.com">

<xs:import schemaLocation="added.xsd" namespace="http://custombookshop.com"/>
<xs:redefine schemaLocation="original.xsd">
    <xs:complexType name="Book">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element ref="cs:Publisher" minOccurs="1" maxOccurs="1" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:redefine>
</xs:schema>

test.xml

<?xml version="1.0"?>
<Book xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="http://bookshop.com"
targetNamespace="http://bookshop.com"
xs:schemaLocation="newMainfile.xsd"
xmlns:cs="http://custombookshop.com">
<Author>Frank Herbert</Author>
<Title>Dune</Title>
<cs:Publisher>Ace</cs:Publisher>
</Book>

验证代码(C#)

var path = @"test.xml";
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add("", "newMainfile.xsd");
XmlReader rd = XmlReader.Create(path);
XDocument doc = XDocument.Load(rd);
doc.Validate(schema, ValidationEventHandler);

错误信息

  1. 首次运行抛出异常:

System.Xml.Schema.XmlSchemaException: 'The targetNamespace parameter '' should be the same value as the targetNamespace 'http://bookshop.com' of the schema.'

  1. 将schema.Add中的空字符串替换为"http://bookshop.com"后,抛出新错误:

System.Xml.Schema.XmlSchemaException: ''SchemaLocation' must successfully resolve if <'redefine'> contains any child other than <'annotation'>.'


疑问

请问我是否遗漏了关键步骤?

内容的提问来源于stack exchange,提问作者S Yuki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 17:13:19