XML Schema:包含仅包含文本的属性的元素?

时间:2008-12-18 00:59:26

标签: xml xsd

我很难找到这个。如何在XML模式文件中为XML定义一个如下所示的元素:

<option value="test">sometext</option>

我无法弄清楚如何定义xs:string类型的元素并且还有一个属性。

这是我到目前为止所得到的:

<xs:element name="option">
    <xs:complexType>
        <xs:attribute name="value" type="xs:string" />
    </xs:complexType>
</xs:element>

3 个答案:

答案 0 :(得分:159)

尝试

  <xs:element name="option" type="AttrElement" />

  <xs:complexType name="AttrElement">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="value" type="xs:string">
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

答案 1 :(得分:71)

...或内联等价物:

<xs:element name="option">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="value" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

答案 2 :(得分:-3)

我知道它不一样,但它对我有用:

<xsd:element name="option">
    <xsd:complexType mixed="true">
        <xsd:attribute name="value" use="optional" type="xsd:string"/>
    </xsd:complexType>
</xsd:element>
相关问题