Sukhinderpal Mann
Sukhinderpal Mann

Reputation: 884

How to stop an element in XML from being empty using XSD

I want empty elements to not be accepted while XML validation using my XSD

My XML:

<Request>
<Query>Select * from Table</Query>
<Query></Query>
</Request>

My XSD:

<xs:simpleType name="className">
    <xs:restriction base="xs:string">
        <xs:minLength value="1" />
    </xs:restriction>
</xs:simpleType>
<xs:simpleType name="status">
    <xs:restriction base="xs:integer">
        <xs:minInclusive value="100" />
        <xs:maxExclusive value="600" />
    </xs:restriction>
</xs:simpleType>
<xs:simpleType name="requires">
    <xs:restriction base="xs:string">
        <xs:minLength value="1" />
    </xs:restriction>
</xs:simpleType>
<xs:complexType name="sql">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="requires" type="requires"/>
            <xs:attribute name="when" type="xs:string" />
            <xs:attribute name="limit" type="xs:string" />
            <xs:attribute name="offset" type="xs:string" />
            <xs:attribute name="classname" type="className" />
            <xs:attribute name="status" type="status"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
<xs:complexType name="query">
    <xs:complexContent>
        <xs:extension base="sql">
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:element name="Request">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Query" type="query" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

I want the query tag to be not accepted if it is empty, so this and should be invalid. Hope my question is to the point. :)

Upvotes: 1

Views: 519

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

You've already got a simpleType className that defines a non-empty string. Rename it to non-empty-string to reflect the intent, then change the sql type do define it as an extension of non-empty-string rather than of xs:string.

Upvotes: 2

Gowri Pranith Kumar
Gowri Pranith Kumar

Reputation: 1685

use the attribute minOccurs=1 on the element in the schema if you want it to be mandatory

Upvotes: 0

Related Questions