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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With