【问题标题】:XML Schema: Define default value in child complextypeXML Schema:在子复杂类型中定义默认值
【发布时间】:2013-07-17 09:57:15
【问题描述】:

我们有一个父元素,它有一个“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>

要创建具有附加属性的子元素,我们需要使用“扩展”。要定义默认值,我们需要使用“限制”。但是,这两者是相互排斥的。

谁能帮我弄清楚怎么做?

提前致谢

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    可以同时实现“扩展”和“限制”的效果 通过一些中间类型:

    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>
    

    但请注意以下几点:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-21
      • 2011-06-16
      • 2010-12-07
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2014-12-31
      • 2021-12-17
      相关资源
      最近更新 更多