XML Schema: Element with attributes containing only text?

Wilco picture Wilco · Dec 18, 2008 · Viewed 92.1k times · Source

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>

Answer

David Norman picture David Norman · Dec 18, 2008

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>