Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting empty elements in xsd

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/>.

like image 867
Chris Welsh Avatar asked Mar 08 '10 08:03

Chris Welsh


People also ask

What is Nillable in XSD?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.

How do I allow null values in XSD?

Answers. The proper way with the W3C XML schema language is using nillable="true" in the schema and xsi: nil="true" in the XML instance document e.g.

What is unbounded XSD?

The maximum number of times an element may appear is determined by the value of a maxOccurs attribute in its declaration. This value may be a positive integer such as 41, or the term unbounded to indicate there is no maximum number of occurrences.


2 Answers

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>
like image 106
Travis Gockel Avatar answered Sep 19 '22 04:09

Travis Gockel


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]

like image 42
InfantPro'Aravind' Avatar answered Sep 21 '22 04:09

InfantPro'Aravind'