【问题标题】:How to Import XSD and add extension to Element如何导入 XSD 并向 Element 添加扩展
【发布时间】: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


    【解决方案1】:

    您不能扩展 xs:element,只能扩展 xs:complexType。 您可以引用在xs:schema 元素中定义的属性(即根属性),但您必须使用其目标命名空间对其进行限定。 此外,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>
    

    Main.xsd

    <?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>
    

    【讨论】:

    • 关于 http 与 https 不匹配的问题,这只是编辑我的示例时的拼写错误,但感谢您注意到这些错误,因为这些错误很容易被忽略。您回答了我的另一个问题,即 Elements 不能像 ComplexTypes 那样可扩展,所以听起来我有点纠结,如果不编辑我希望导入的主 xsd 文件就没有可以实现的解决方案。感谢您的回复以及有关 XSD 的课程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多