xsd schema declaring xml only with attributes

For schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.bawi.com/schema" targetNamespace="http://www.bawi.com/schema" elementFormDefault="qualified">
  <xs:element name="MyRootElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="MyElement">
          <xs:complexType>
            <xs:attribute name="myAttribute"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Screen Shot 2017-07-27 at 22.06.15
we have valid xml:

<?xml version="1.0" encoding="UTF-8"?>
<MyRootElement xmlns="http://www.bawi.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bawi.com/schema schema.xsd">
  <MyElement myAttribute="anyType"/>
</MyRootElement>

but when we add text body (e.g. ‘aaa’) to the xml:

<?xml version="1.0" encoding="UTF-8"?>
<MyRootElement xmlns="http://www.bawi.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bawi.com/schema schema.xsd">
  <MyElement myAttribute="anyType">aaa</MyElement>
</MyRootElement>

then we get validation error:

lineNumber: 3; columnNumber: 51; cvc-complex-type.2.1: Element 'MyElement' must have no character or element information item [children], because the type's content type is empty.

In order to fix it we need to add simepleContent and extension to the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.bawi.com/schema" targetNamespace="http://www.bawi.com/schema" elementFormDefault="qualified">
  <xs:element name="MyRootElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="MyElement">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="myAttribute"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Then both xml above with be valid.
Note text-like information in the MyElement icon:
Screen Shot 2017-07-27 at 22.19.43

Empty elements such as

<MyElement/>

is allowed when schema defines it as

<xs:element name="MyElement"/>

or

<xs:element name="MyElement" type="xs:string"/>

. However, when we change schema to int type such as

<xs:element name="MyElement" type="xs:int"/>

then we get validation error:

lineNumber: 3; columnNumber: 15; cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s