Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XSD how do I allow only whitespace in an element's content?

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?

like image 620
Dave Avatar asked May 04 '18 15:05

Dave


People also ask

What is simpleType in XSD?

XSD Elements. XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is the difference between element and attribute in XSD?

An element is an XML node - and it can contain other nodes, or attributes. It can be a simple type or a complex type. It is an XML entity. An attribute is a descriptor.

What is Nillable in XSD?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.

What does Xs mean in XSD?

For brevity, the text and examples in this specification use the prefix xs: to stand for this namespace; in practice, any prefix can be used. in the end xs or xsd are only prefixes. XSD is used for example more by Microsoft schemas. The important is how you declare the namespace.


1 Answers

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

like image 79
kjhughes Avatar answered Sep 17 '22 22:09

kjhughes