Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD attribute (Not an element) should not be an empty string [duplicate]

Tags:

xml

xsd

What changes I need to make in below defined schema, so that attribute named code should not be an empty string/validate if code is empty?

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:attribute name="code" type="xsd:string"/>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Child" nillable="false">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="childAge">
                                <xsd:simpleType>
                                    <xsd:restriction base="xsd:integer"/>
                                </xsd:simpleType>
                            </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute ref="code" use="required"></xsd:attribute>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
</xsd:element>

like image 939
Param Bhat Avatar asked Mar 09 '14 14:03

Param Bhat


1 Answers

Type xsd:string type includes the empty string, so using

<Child code="">

Is valid according to your schema. There are several ways to restrict the type. If you just want to restrict the length you could use:

<xsd:attribute name="code">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:attribute>

Or you can use a type that does not include the empty string as valid, for example:

<xsd:attribute name="code" type="xsd:NMTOKEN" />

which also won't allow special characters or spaces. If your code requires a specific pattern, you might want to specify that in a regular expression, for example:

<xsd:restriction base="xsd:string">
    <xsd:pattern value="[A-Z][0-9]{4}"/>
</xsd:restriction>

which will also not validate for empty strings.

like image 58
helderdarocha Avatar answered Nov 15 '22 23:11

helderdarocha