1

I need a sample XSD to support multiple email recipients in a new element. I require each recipient email address in a different element. Can anyone help me with explanation?

Example:

<EmailReceipts> 
    <address1></address1>
    <address2></address2>
</EmailReceipts>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Mohan
  • 59
  • 1
  • 7

1 Answers1

1

First off, I'd recommend not embedding an index number in the address elements:

<EmailReceipts> 
  <address>john@example.com</address>
  <address>mary@example.org</address>
</EmailReceipts>

Then this XSD will validate the above XML (as well as other XML documents with additional address elements):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="EmailReceipts">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="address" maxOccurs="unbounded" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The above XSD will allow any string contents for the address elements. If you've like to be more strict, you could use a regular expression to limit the values for address:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="EmailReceipts">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="address" maxOccurs="unbounded" type="EmailAddressType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="EmailAddressType">
    <xs:restriction base="xs:string">
      <xs:pattern value="([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Note that the above regular expression is one of many possible, each having various degrees of generality and specificity over a syntax that is more involved than you might imagine.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240