Ben5486
Ben5486

Reputation: 51

How to validate not empty value of complexType with simpleContent

I have an XML file with the following content:

<farm>
    <propertybag name="name1">Value1</propertybag>
    <propertybag name="name2">Value2</propertybag>
    <propertybag name="name3"></propertybag>
</farm>

I just want to set my XSD file to not validate the third element name3 because there is no value.

Here is my XSD file:

<xs:element name="farm" minOccurs="0" maxOccurs="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="propertybag" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="name" type="xs:string" use="required" />
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

I tried with usual:

<xs:restriction base="xs:string">
    <xs:minLength value="1"/>
    <xs:whiteSpace value="collapse"/>
</xs:restriction>

But it doesn't work, I tried several solutions but without success. Any help is welcomed.

Upvotes: 0

Views: 178

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

Where you have <xs:extension base="xs:string"> you need instead to declare the base type as say base="my:non-empty-string", where non-empty-string is defined as a type that restricts xs:string with minLength="1".

Upvotes: 1

Ben5486
Ben5486

Reputation: 51

So I finally found a solution, I created a simpleType to validate attribute and element to not be empty or whitespace :

<xs:element name="farm" minOccurs="0" maxOccurs="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="propertybag" type="propertyBag" minOccurs="1" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>



<xs:simpleType name="propertyValue">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
        <xs:whiteSpace value="collapse"/>
    </xs:restriction>
</xs:simpleType>

Upvotes: 0

Related Questions