Reputation: 21492
I needed to define an XML element that has no sub-elements or any content at all, and has no attributes.
This is what I am doing:
<xs:element name="myEmptyElement" type="_Empty"/>
<xs:complexType name="_Empty">
</xs:complexType>
This appears to work fine, but I have to wonder if there is a way to do this without having to declare a complex type. Also, if there is anything wrong with what I have, please let me know.
Anticipating that someone might be curious why I would need such an element: It is for a SOAP operation that does not require any parameter values.
Upvotes: 30
Views: 29731
Reputation: 51
Another example could be:
<xs:complexType name="empty">
<xs:sequence/>
</xs:complexType>
<xs:element name="myEmptyElement" type="empty>
or
<xs:element name="myEmptyElement">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
or
<xs:element name="myEmptyElement">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:anyType"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
Upvotes: 4
Reputation: 111726
(1) You could avoid defining a named xs:complexType
:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myEmptyElement">
<xs:complexType/>
</xs:element>
</xs:schema>
(2) You could use a xs:simpleType
instead of a xs:complexType
:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myEmptyElement">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
(3) You could use fixed=""
[credit: @Nemo]:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myEmptyElement" type="xs:string" fixed=""/>
</xs:schema>
(4) But note that if you avoid saying anything about the content model:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myEmptyElement"/>
</xs:schema>
You'll be allowing any attributes on and any content in myEmptyElement
.
Upvotes: 50