etxalpo
etxalpo

Reputation: 1176

How to prevent empty element in XML via XSD

I'm a newbie when it comes to xml schemas. Anyway here goes my question:

I have the following element

<property name="propA">some-value</property>

and I would like my XSD to prevent empty elements, such as this:

<property name="propB" />
<property name="propC"></property>

How can I achieve this with my current XSD, as showed below:

<xs:complexType name="property">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" use="required" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

Upvotes: 1

Views: 2400

Answers (1)

Vincent Biragnet
Vincent Biragnet

Reputation: 2998

You can build your own simpleType for this. For example :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 
    <xs:element name="property" type="property"/>
    <xs:complexType name="property">
        <xs:simpleContent>
            <xs:extension base="nonEmptyString">
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:simpleType name="nonEmptyString">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Upvotes: 1

Related Questions