Tag: validation

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

XSD schema validation of XML

Note the schema and the generated valid xml below (demonstrating different xsd schema syntax features):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="MyRootElement">
		<xs:annotation>
			<xs:documentation>Comment describing your root element</xs:documentation>
		</xs:annotation>
		<xs:complexType>
			<xs:sequence>
				<xs:element name="MySubElement"/>
				<xs:element name="MySubElementString" type="xs:string"/>
				
				<!-- by default elements are required -->
				<xs:element name="MySubElementOptional" minOccurs="0"/>

				<xs:element name="MySubElementRequiring3CharString">
					<xs:simpleType>
					
						<!-- restriction needs to have base type -->
						<xs:restriction base="xs:string">
							<xs:length value="3"/>
						</xs:restriction>
					</xs:simpleType>
				</xs:element>
				
				<xs:element name="MySubElementDisallowingText">
					<xs:complexType>
					</xs:complexType>
				</xs:element>
				
				<xs:element name="MySubElementWithOptionalAttrAndOptionalText" type="MySubElementWithOptionalAttrAndOptionalText" />

			</xs:sequence>
			
			<!-- attribute needs to be below sequence -->
			
			<xs:attribute name="myAttribute" type="xs:integer"/>
			
			<!-- by default attributes are optional -->
			<xs:attribute name="myRequiredAttribute" type="xs:integer" use="required"/>
			
		</xs:complexType>
	</xs:element>
	
	<xs:complexType name="MySubElementWithOptionalAttrAndOptionalText">
		<xs:simpleContent>
			<xs:extension base="xs:string">
				<xs:attribute name="myIntegerAttribute" />
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
</xs:schema>

Here is a sample xml (valid to the schema above)

<?xml version="1.0" encoding="UTF-8"?>
<MyRootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" myAttribute="0" myRequiredAttribute="0" xsi:noNamespaceSchemaLocation="my-schema.xsd">
	<MySubElement>text</MySubElement> <!-- can be replaced with: <MySubElement /> -->
	<MySubElementString>String</MySubElementString> <!-- can be replaced with: <MySubElementString /> -->
	<MySubElementOptional>text</MySubElementOptional> <!-- can be completely removed -->
	<MySubElementRequiring3CharString>aaa</MySubElementRequiring3CharString>
	<MySubElementDisallowingText/>
	<MySubElementWithOptionalAttrAndOptionalText myIntegerAttribute="text">String</MySubElementWithOptionalAttrAndOptionalText>
	<!-- can be replaced with: 	<MySubElementWithOptionalAttrAndOptionalText /> -->
</MyRootElement>

my-schema.xsd