Jump to content

Optional element with required length, value, or pattern?

Guest rich t

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...