【发布时间】:2020-03-06 00:06:12
【问题描述】:
编辑:根据我的阅读,我认为我需要尝试这样的事情...How to extend a complex type in a different namespace without changing name我还没有尝试过,但如果我用这种方法成功的话会更新...
所以我有一个项目,我有一个由第三方提供的大型 XSD,我想将我自己的元素和属性添加到 XSD。我想在substitutiongroup="SpecificResource" 中添加一个元素以及一个属性。
我们将较大的第三方 xsd 称为“base.xsd”,我已将其剥离为我认为与示例相关的部分。
<xs:schema targetNamespace="http://www.base.com"
xmlns="http://www.base.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element abstract="true" name="SpecificResource" type="SpecificResource"/>
<xs:complexType abstract="true" name="SpecificResource">
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:complexType>
<xs:element name="FileSpec">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" ref="Disposition"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="NetworkHeader"/>
</xs:sequence>
<xs:attribute name="Example" type="xs:string" use="optional"/>
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:complexType>
</xs:element>
我的自定义 xsd 名为“Custom.xsd”
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cus="https://www.custom.com"
xmlns="http://www.base.com"
targetNamespace="https://www.custom.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="https://www.base.com" schemaLocation="base.xsd"/>
<xs:attribute name="URL" type="xs:string"/>
<xs:attribute name="customattribute" type="xs:NMTOKEN"/>
<xs:element name="SpecRes1" substitutionGroup="SpecificResource" type="cus:SpecRes1"/>
<xs:complexType name="SpecRes1">
<xs:complexContent>
<xs:extension base="SpecificResource">
<xs:attribute ref="cus:customattribute"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name ="fullfilespec">
<xs:complexContent>
<xs:extension base="FileSpec">
<xs:attribute ref="URL" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
但是,现在我得到“Undefined complexType 'http://www.base.com:FileSpec' is used as a base for complex type extension.”我不明白为什么,因为我认为 Filespec 是在 base.xsd 中定义的。
在导入时我做错了吗?
一个后续问题是是否有办法使导入的 XSD 不需要前缀,而使我添加的项目需要命名空间前缀 (cus:) 目前我已经能够将我的元素添加到“SpecificResource”但我的元素似乎是在 base.xsd 命名空间中定义的,我认为这不是我想要的。当我使用 XSD 时,我最终得到一个有效的文件,而没有在我添加的元素上添加前缀,这使得很难区分我添加的内容和已经存在的内容。 (我正在从 xsd 生成一个类文件,并用它对数据进行序列化和反序列化。)
我采用了这种导入基本 xsd 的方法,这样我就不需要接触 base.xsd 文件,但是不能在自定义命名空间中扩展导入的元素吗?
我最初是添加到 base.xsd 并导入我的 custom.xsd,但继续添加到 base.xsd 变得非常乏味,同时也使添加到替代组 SpecificResource 变得困难。
【问题讨论】:
标签: xsd