XML架构:在子complextype中定义默认值



我们有一个具有"implementationClass"属性的父元素。这个父元素有许多子元素,我们希望在其中定义一个默认值。以下是父元素的定义:

<xsd:complexType name="serviceType">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:ID" />
        <xsd:element name="connection">
            <xsd:complexType>
                <xsd:attribute name="reference" type="xsd:IDREF" use="required" />
            </xsd:complexType>
        </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="implementationClass" type="xsd:string" use="optional" />
</xsd:complexType>

要创建具有additionnal属性的子元素,我们需要使用"extension"。要定义默认值,我们需要使用"restriction"。然而,这两个是相互排斥的。

有人能帮我想办法吗?

提前感谢

您可以同时实现"扩展"one_answers"限制"的效果通过某种中间类型:

serviceType -> serviceTypeIntermediate -> serviceTypeResult

例如:

<xsd:complexType name="serviceTypeIntermediate">
  <xsd:complexContent>
    <xsd:extension base="serviceType">
      <xsd:sequence>
        <xsd:element name="something" type="xsd:String"/>
      <xsd:sequence>
    <xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="serviceTypeResult">
  <xsd:complexContent>
    <xsd:restriction base="serviceTypeIntermediate">
      ...
    <xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>

但请注意以下内容:

(复杂内容的)扩展工作,以便新的子元素(在其中声明)作为序列添加到初始内容。也就是说,不能"扩展"那些已经在基类型中声明的子级。

限制中,必须重新定义整个内容模型。但新内容必须与基本类型中定义的内容完全兼容。

最新更新