XML Schema: Element with attributes containing only text?

XmlXsd

Xml Problem Overview


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>

Xml Solutions


Solution 1 - Xml

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>

Solution 2 - Xml

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

Solution 3 - Xml

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilcoView Question on Stackoverflow
Solution 1 - XmlDavid NormanView Answer on Stackoverflow
Solution 2 - XmlJulian HView Answer on Stackoverflow
Solution 3 - XmlAitorView Answer on Stackoverflow