Paul
Paul

Reputation: 45

Can you have an empty XML element with use="required"

I imagine this is a simple question to answer but using the XSD line below, is the XML line below valid or not?

XSD

<xsd:element name="Something" type="xsd:int" use="required"/>

XML

<Something />

Upvotes: 1

Views: 255

Answers (1)

Sprotty
Sprotty

Reputation: 5973

use="required" applies to attributes not elements. By default an element is required (minOccurs defaults to 1). If you want to change this set minOccurs/maxOccurs.

For example

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2017 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="d">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Item" type="xs:int" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:attribute name="myAttribute" type="xs:int" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 1

Related Questions