如何导入XSD并向元素添加扩展

如何导入XSD并向元素添加扩展,xsd,Xsd,编辑:根据我的阅读,我想我需要试试这样的东西。。。我还没有尝试,但将更新,如果我成功与此方法 所以我有一个项目,我有一个由第三方提供的大型XSD,我想向XSD添加我自己的元素和属性。我想向substitutiongroup=“SpecificResource”添加一个元素和一个属性 让我们将更大的第三方xsd称为“base.xsd”,我已经将其拆分为我认为与示例相关的部分 <xs:schema targetNamespace="http://www.base.com"

编辑:根据我的阅读,我想我需要试试这样的东西。。。我还没有尝试,但将更新,如果我成功与此方法

所以我有一个项目,我有一个由第三方提供的大型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”


但是,现在我得到了“Undefined complexType“”被用作复杂类型扩展的基础。”我不明白为什么我认为Filespec是在base.xsd中定义的

我在进口方面做错了什么吗

接下来的问题是,是否有办法使导入的XSD不需要前缀,同时使我添加的项需要名称空间前缀(cus:),目前我已经能够将我的元素添加到“SpecificResource”中,但我的元素似乎是在base.XSD名称空间中定义的,我认为这不是我想要的。当我使用XSD时,我得到的文件是有效的,没有在我添加的元素上添加前缀,这使得很难区分我添加的内容和已经存在的内容。(我正在从xsd生成一个类文件,并用它序列化和反序列化数据。)

我采用了这种导入base xsd的方法,这样我就不需要接触base.xsd文件,但是在自定义名称空间中的导入元素上扩展是不可能的吗


我最初是添加到base.xsd并导入我的custom.xsd的,但是在添加到base.xsd的同时也使添加到替换组SpecificResource变得非常繁琐。

您不能扩展
xs:element
您只能扩展
xs:complexType
。 您可以引用
xs:schema
元素中定义的属性(即根属性),但必须使用其targetnamespace对其进行限定。 另外,
xs:import
中的名称空间必须与源xsd文件匹配(您的名称空间与http/https不匹配)

Base.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!-- Created with Liquid Studio 2020 (https://www.liquid-technologies.com) -->
<xs:schema xmlns="http://www.base.com" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.base.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="SpecificResource" type="SpecificResource" abstract="true" />
    <xs:complexType abstract="true" name="SpecificResource">
        <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
    <xs:complexType name="FileSpec">
        <xs:sequence>
            <xs:element name="Disposition" type="xs:string" minOccurs="0" maxOccurs="1" />
            <xs:element name="NetworkHeader" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Example" type="xs:string" use="optional" />
        <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema xmlns:cus="https://www.custom.com" xmlns="http://www.base.com" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="https://www.custom.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation=".\base.xsd" namespace="http://www.base.com" />
    <xs:attribute name="URL" type="xs:string" />
    <xs:attribute name="customattribute" type="xs:NMTOKEN" />
    <xs:element name="SpecRes1" type="cus:SpecRes1" substitutionGroup="SpecificResource" />
    <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="cus:URL" use="optional" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

Main.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!-- Created with Liquid Studio 2020 (https://www.liquid-technologies.com) -->
<xs:schema xmlns="http://www.base.com" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.base.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="SpecificResource" type="SpecificResource" abstract="true" />
    <xs:complexType abstract="true" name="SpecificResource">
        <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
    <xs:complexType name="FileSpec">
        <xs:sequence>
            <xs:element name="Disposition" type="xs:string" minOccurs="0" maxOccurs="1" />
            <xs:element name="NetworkHeader" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Example" type="xs:string" use="optional" />
        <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema xmlns:cus="https://www.custom.com" xmlns="http://www.base.com" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="https://www.custom.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation=".\base.xsd" namespace="http://www.base.com" />
    <xs:attribute name="URL" type="xs:string" />
    <xs:attribute name="customattribute" type="xs:NMTOKEN" />
    <xs:element name="SpecRes1" type="cus:SpecRes1" substitutionGroup="SpecificResource" />
    <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="cus:URL" use="optional" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

Oops关于http与https的不匹配,这只是编辑我的示例时的一个输入错误,但感谢您注意到,因为这些类型的错误很容易被忽略。你回答了我的另一个问题,关于元素不能像ComplexTypes那样可扩展,所以听起来我有点困惑,没有一个解决方案可以在不编辑我希望导入的主xsd文件的情况下实现。谢谢你的回复,还有关于XSD的课程。