Restricting empty elements in xsd

17,860

Solution 1

If you're trying to prevent the element from appearing at all, you can mark it with maxOccurs="0". I'm guessing this isn't what you're after, so if you're trying to make sure there are always attributes attached to the complex element, then you have to specify usage="required" on at least one of the attributes or use an attribute group. If myElement is a simple type and you want to make sure it has a value, then you could always restrict the type of it. If you want a non-zero string, then you could do:

<xsd:element name="myElement">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

Solution 2

If your schema-validation isn't able to show error, when an Element of data-type DATE is null, then you can use a pattern [if it isn't a burden for you to type the required format];

I have added an example, implementation of similar code would work on your tool,

This is the sample XML:

<root>
   <date1>12/31/1999</date1> <!-- The Date format defined here is MM/DD/YYYY, null value or Date with any other format aren't accepted-->
</root>

This is the corresponding XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="Date_Def.xsd"/>
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="date1" type="DATE_TYPE" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Observe that I am including one more schema file which includes the definition of type DATE_TYPE,
Here is the Date_Def.xsd file:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="DATE_TYPE">
    <xs:restriction base="xs:string">
      <xs:pattern value="([0][1-9]|[1][0-2])/([0][1-9]|[1-2][0-9]|[3][0-1])/[1-2][0-9][0-9][0-9]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

The Date format defined here is MM/DD/YYYY, null value or Date with any other format aren't accepted, If you want to accept also a null tag, the replace pattern with this ..

<xs:pattern value="|(([0][1-9]|[1][0-2])/([0][1-9]|[1-2][0-9]|[3][0-1])/[1-2][0-9][0-9][0-9])"/>

Validation of which accepts, either null tag or a Date-value of pattern MM/DD/YYYY.

If you need more help on design of patterns, then feel free to make it a post in SO, hope it helped. :-)

[note :: The Type-Definition can also be defined in a same file, which needs additional name-spaces mentioned in XML as well as XSD files, defining an external file is harmless and re-usable]

Share:
17,860
Chris Welsh
Author by

Chris Welsh

Updated on June 27, 2022

Comments

  • Chris Welsh
    Chris Welsh almost 2 years

    Is there a way to prevent empty elements of the form <myElement/> being used in your xml? In other words, can you specify in your xsd that <myElement/> is invalid?

    Using nillable="false" doesn't work, nor does minOccurs="1" - both of those allow <myElement/>.