【问题标题】:XSD overriding parentXSD 覆盖父级
【发布时间】:2022-01-19 09:28:57
【问题描述】:

我正在使用两个架构文件,parent.xsd 和 child.xsd,其中包括父架构。

在父级内部,我已经通过以下方式定义了一个非常简单的元素

<xs:element name="parentElement">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="1">
      <xs:element ref="another_element" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
  <xs:attribute name="firstAttr" type="attrType"/>            
 </xs:complexType>
 </xs:element>

现在,在子架构中,我想覆盖这个元素并添加一个新属性。孩子应该与父母同名。这可以在 XSD 中完成吗?

我已经尝试过 但我想真正使用相同的父元素,而不是基于父元素定义新元素。

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    首先您需要提取复杂类型。

    <xs:element name="parentElement" type="typeOfParentElement"/>
    
    <xs:complexType name="typeOfParentElement">
        <xs:sequence minOccurs="0" maxOccurs="1">
          <xs:element ref="another_element" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
      <xs:attribute name="firstAttr" type="attrType"/>            
    </xs:complexType>
    

    然后您将需要定义一个扩展 typeOfParentElement 并带有附加属性的类型。

    然后你需要用这个扩展类型定义子元素的类型。

    从您的描述中不清楚子元素是否与父元素同名。如果是这样,您将需要在局部元素声明而不是全局元素声明中定义它,因为您不能有两个同名的全局元素。

    【讨论】:

    • 感谢您的回复!是的,孩子应该和父母同名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2022-12-18
    • 2014-06-08
    • 1970-01-01
    • 2011-05-14
    • 2012-10-07
    • 2011-01-30
    相关资源
    最近更新 更多