XSD Text-Only Elements

Both text and attributes can be included in a complex text-only element.

Complex Text-Only Elements:

We can add a simpleContent element around the content because the complex text-only element contains only simple content (text and attributes). We need to define an extension or a restriction within the simpleContent element while using simple content.

Syntax 1:

<xs:element name="name">
  <xs:complextype>
    <xs:simplecontent>
      <xs:extension base="basetype">
       ....
       ....
      </xs:extension>
    </xs:simplecontent>
  </xs:complextype>
</xs:element>

Syntax 2:

<xs:element name="name">
  <xs:complextype>
    <xs:simplecontent>
      <xs:restriction base="basetype">
       ....
       ....
      </xs:restriction>
    </xs:simplecontent>
  </xs:complextype>
</xs:element>

To expand or to limit the base simple type for the element, we can use the extension/restriction element.

Example 1:

<cricketjersey country="india">18</cricketjersey>

Explanation:

In the above example, we are defining an XML element “cricketjersey”, that contains text-only.

Example 2:

<xs:element name="cricketjersey">
  <xs:complextype>
    <xs:simplecontent>
      <xs:extension base="xs:integer">
        <xs:attribute name="country" type="xs:string"></xs:attribute>
      </xs:extension>
    </xs:simplecontent>
  </xs:complextype>
</xs:element>

Explanation:

In the above example, we are defining an XML element “cricketjersey” as a complexType whose content is defined as an integer value. This element also contains an attribute named “country”.

Example 3:

<xs:element name="cricketjersey" type="crickettype"></xs:element>
 
<xs:complextype name="crickettype">
  <xs:simplecontent>
    <xs:extension base="xs:integer">
      <xs:attribute name="country" type="xs:string"></xs:attribute>
    </xs:extension>
  </xs:simplecontent>
</xs:complextype>

Explanation:

In the above example, we are giving the complexType element a name. Here, the “cricketjersey” element also has a type attribute that refers to the name of the complexType. Several elements can refer to the same complex type, by using this method.

Please Share