Jump to content

Optional element with required length, value, or pattern?


Guest rich t
 Share

Recommended Posts

I have a simple type declared in one schema (see first snippet below). The purpose of this schema is to define types reusable in many other schemas. These other schemas define the sequence of elements for various XML documents.<xsd:simpleType name="delete_type"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1"/> <xsd:enumeration value="N"/> <xsd:enumeration value="D"/> </xsd:restriction></xsd:simpleType>Within one of my document structure schemas, I include the above schema using the below.<xsd:include schemaLocation="file:///c:/temp/PROPS.xsd">Below is what one my document structure schemas looks like. I want to make one or more of these elements optional. I try to do so by adding minOccurs="0"; however, XML validation continually fails when the element has no data. The problem is that even though minOccurs="0", validation still fails because either the minLength is set to 1 or the required pattern or enumeration is not satisfied. How can I make a field optional, but when there is data present, require it to have a certain length, value, or pattern?<xsd:element name="document_item"> <xsd:complexType> <xsd:sequence> <xsd:element name="Delete" type="delete_type" minOccurs="0"/> <xsd:element name="Document_Number" type="doc_num_type"/> <xsd:element name="Document_Title" type="doc_title_type"/> <xsd:element name="Deck"/> <xsd:element name="Skid" type="skid_type"/> <xsd:element name="Revision" type="revision_type"/> <xsd:element name="Revision_Date" type="revision_date_type"/> <xsd:element name="Originator" type="originator_type"/> <xsd:element name="Originator_Document_Number" type="originator_doc_num_type"/> <xsd:element name="Vendor" type="vendor_type"/> <xsd:element name="Vendor_Document_Number" type="vendor_doc_num_type"/> </xsd:sequence> </xsd:complexType></xsd:element>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...