XSD attribute (not element) must not be an empty string

What changes do I need to make in the diagram below, so the attribute with the name code should not be an empty string / check if the 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> 

+7
xml xsd
source share
1 answer

The xsd:string includes an empty string, so use

 <Child code=""> 

Acts in accordance with your scheme. There are several ways to restrict type. If you just want to limit the length that 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 an empty string as a valid one, for example:

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

which also will not allow the use of special characters or spaces. If your code requires a specific pattern, you can specify it in a regular expression, for example:

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

which will also not check for empty lines.

+11
source share

All Articles