Attribute definitions

Figure 708. Using a birthDate attribute Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="email"
          minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="birthDate"
      type="xs:date" use="optional"/>
  </xs:complexType>
</xs:element>
<contact ...="organizer.xsd"
  birthDate="1995-11-29">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>
</contact>

Figure 709. Absent birthDate attribute Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="email"
        minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="birthDate"
     type="xs:date" use="optional"/>
  </xs:complexType>
</xs:element>
<!-- birthDate is optional-->
<contact ...="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>
</contact>

Figure 710. XSD attributes revisited Slide presentation Create comment in forum
XSD attributes revisited

Figure 711. Consider <email> Slide presentation Create comment in forum
<contact ...="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>

  <!-- Good idea, but no «real» email -->
  <email>dexter at my dot com</email>
</contact>
  • Basic validation desired.

  • No «email» data type in XSD.

  • Solution: User defined data type.

    Restrict an existing XML Schema type by a regular expression.


Figure 712. User defined email type Slide presentation Create comment in forum
<xs:simpleType name="emailType" >
  <xs:restriction  base="xs:string"  >
    <xs:pattern value=
    "[_\-a-zA-Z0-9\.\+]+@[a-zA-Z0-9](\.?[\-a-zA-Z0-9]*[a-zA-Z0-9])*"/>
                                                                  
  </xs:restriction>
</xs:simpleType>

Even better: https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression

Creating a user defined data type. The @name attributes value emailType acts as a key for referencing purposes.

Our new type will restrict an existing type.

Our new type will be derived from xs:string.

Note: This is related to the concept of inheritance: Instances of our newly defined type can still be conceived as xs:string instances. The converse is not true.

A regular expression defining valid email addresses. Note the @ sign at position 20 appearing exactly once.


Figure 713. Using new email type Slide presentation Create comment in forum
<xs:simpleType name="emailType" ◀━━━━┓ >
...value="[_\-a-zA-...0-9])*"/> ...  
</xs:simpleType>                     
                                     
<xs:element name="contact">          
  <xs:complexType>                   
    <xs:sequence>                    
    ...                              
      <xs:element name="email"       
    minOccurs="0" type="emailType" ━━┛ />
    </xs:sequence> ...
  </xs:complexType>
</xs:element>
<contact ...="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>

  <!-- Value 'dexter at my dot com'
       is not facet-valid
       with respect to pattern
       '[_\-a-z...0-9])*'
        for type 'emailType'. -->
  <email>dexter at my dot com</email>
</contact>

User defined type definition emailType.

Referencing user defined type.

Figure 714. Strange: The Ur type Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName" />
      <xs:element name="lastName" />
      <xs:element name="email" .../>
    </xs:sequence>               ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    <xs:attribute                ┃  <contact ...="organizer.xsd">
       name="birthDate" .../>    ┃   <firstName>Eve <email>eve@y.com</email> </firstName>
  </xs:complexType>              ┃   <lastName>Dexter <pre>Gordon</pre>  </lastName>
</xs:element>                    ┃  </contact>

Absent type="..." declaration for firstName and lastName.

Element <email> likely not to appear inside <firstName>!

Even worse: Element <pre> is not even defined in organizer.xsd!


Figure 715. Adding missing data types Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName" type="xs:string"/>
      <xs:element name="lastName" type="xs:string"/>
      <xs:element name="email" minOccurs="0" type="emailType"/>
    </xs:sequence>
    <xs:attribute name="birthDate" type="xs:date" use="optional"/>
  </xs:complexType>
</xs:element>

Figure 716. Nested elements becoming invalid Slide presentation Create comment in forum
<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="organizer.xsd"
  birthDate="1995-11-29">

  <!-- Wrong: type string -->
  <firstName>Eve <email>eve@paradise.com</email></firstName>
  <lastName>Dexter<p>Gordon</p></lastName>
</contact>

Element 'firstName' is a simple type, so it must have no element information item [children].

Element 'lastName' is a simple type, so it must have no element information item [children].


Figure 717. Introducing categories Slide presentation Create comment in forum
<-- @category may be either of:
      -friend
      -colleague
      -businessPartner        -->

<contact birthDate="1995-11-29"
  category="businessPartner">
  <firstName>Eve </firstName>
    <lastName>Dexter</lastName>
</contact>

Figure 718. Category type violation Slide presentation Create comment in forum
<xs:simpleType name="contactCategoryType">
  <xs:restriction base="xs:string">  
    <xs:enumeration value="friend"/> ┗━━┓
    <xs:enumeration value="colleague"/> 
    <xs:enumeration                     
              value="businessPartner"/> 
  </xs:restriction>                     
</xs:simpleType>                        
                                        
<xs:element name="contact">             
  <xs:complexType>                      
    <xs:sequence> ...                   
    </xs:sequence>                      
    <xs:attribute name="birthDate"      
                 type="contactCategoryType"
                   use="optional"/>
  </xs:complexType>
</xs:element>
<contact
  category="author">
  <firstName>...
</contact>
Value 'author' is not
facet-valid with respect
to enumeration'
[friend, colleague,
  businessPartner]'...

Figure 719. Ooops: Text content and attributes Slide presentation Create comment in forum
<a 
href="https://my.com"
   >Some text</a>
  
         
         
 <!-- Just a string -->
<xs:element name="a">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string"> 
        <xs:attribute name="href" 
                   type="xs:anyURI"/> 
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

The <a> element will contain pure text. This is being achieved by choosing <xs:simpleContent>.

The element requires an URL representing attribute <a href="..."> corresponding to xs:anyURI.