Weckar E.
Weckar E.

Reputation: 777

XSD: how to describe an element so it can have NO value?

XML Example with value:

<Type name="firstType">1</Type>

XML Example without value:

<Type name="secondType" />

How does one in XSD define the second type of element? What kind of element type does it have?

Upvotes: 2

Views: 1817

Answers (2)

Sprotty
Sprotty

Reputation: 6003

I think there are a few issues with previous answer.

Using this sample

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2017 (https://www.liquid-technologies.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="ElementInt" type="xs:int" />
                <xs:element name="ElementWithAnything" />
                <xs:element name="myEmptyElm">
                    <xs:complexType />
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Element 'ElementInt' is easy to understand, its type is set to xs:int, so it must contain an int.

Element 'ElementWithAnything' is where it gets interesting, as the type is NOT specified, type defaults to xs:anyType, which allows anything, text, elements, attributes whatever.

Element 'myEmptyElm' is basically saying its type is based on the child xs:complexType. An xs:complexType is empty to start with, so unless you add anything to it, the resulting XML element also has to be empty.

To declare an element that is empty

<xs:element name="myEmptyElm">
    <xs:complexType />
</xs:element>

To declare an element that allows any child text, elements or attributes

<xs:element name="ElementWithAnything" />

Upvotes: 1

ceving
ceving

Reputation: 23871

You define the first element (without the "name" attribute) this way:

<xs:element name="Type" type="xs:integer" />

If you define the second element without a type and without a child sequence or choice

<xs:element name="Type" />

then no child element is allowed. And this means the element must be empty.

But you can not define an element being empty, if it has a specific attribute value, and at the same time being not empty, if it has another attribute value.

This is the complete example with the name attributes:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="type0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:integer">
                <xs:attribute name="name" use="required" fixed="firstType"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="type1">
          <xs:complexType>
            <xs:attribute name="name" use="required" fixed="secondType"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

But instead of defining the types in attributes I would recommend defining the types in elements. This would allow different child types.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="type">
          <xs:complexType>
            <xs:choice>
              <xs:element name="firstType" type="xs:integer"/>
              <xs:element name="secondType"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 1

Related Questions