I am writing the XSD for an element that has no content, only attributes, which seems fairly straightforward:
<xs:complexType name="ViewElement">
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>
<xs:element name="VIEW" type="ViewElement" minOccurs="0" maxOccurs="unbounded"/>
If the XML contains
<VIEW Name='V_UP'></VIEW>
or
<VIEW Name='V_UP'/>
it works fine. But if the XML contains
<VIEW Name='V_UP'>
</VIEW>
I get
The element cannot contain white space. Content model is empty.
I want to allow the authors of the XML the flexibility to write the XML this way, but I can't work out how to allow content, but only whitespace content. Any suggestions?
You can use a xsd:whiteSpace
facet with value="collapse"
and require a length of 0:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="OnlyWhiteSpaceElement">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
Update 1: OP would like to add attributes to OnlyWhiteSpaceElement
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="OnlyWhiteSpaceElement">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="OnlyWhiteSpaceType">
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="OnlyWhiteSpaceType">
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
See also: Restrict complexType with attributes in XSD?
Update 2: OP would like to reuse the currently anonymous complex type. Let's defined a named type and refactor the names being used...
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="OnlyWhiteSpaceElement" type="OnlyWhiteSpaceType"/>
<xs:complexType name="OnlyWhiteSpaceType">
<xs:simpleContent>
<xs:extension base="OnlyWhiteSpaceContentType">
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="OnlyWhiteSpaceContentType">
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Update 3: Be aware of XSD processor implementation differences regarding xs:whiteSpace value="collapse"
.