Wilco
Wilco

Reputation: 33356

XML Schema: Element with attributes containing only text?

I'm having difficulty searching for this. How would I define an element in an XML schema file for XML that looks like this:

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

I can't figure out how to define an element that is of type xs:string and also has an attribute.

Here's what I've got so far:

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

Upvotes: 155

Views: 105230

Answers (3)

Aitor
Aitor

Reputation: 21

I know it is not the same, but it works for me:

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

Upvotes: -1

Julian H
Julian H

Reputation: 1859

... or the inline equivalent:

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

Upvotes: 87

David Norman
David Norman

Reputation: 19879

Try

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

Upvotes: 190

Related Questions