Friday, 7 November 2014

xs : pattern : matches a pattern


<xs:pattern> defines a pattern, that must be matched by given data.

Example
<xs:element name="firstName" >
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-z]*"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>


Above snippet expects firstName as combination of alphabets only. No special characters, no numbers are allowed.

student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="firstName" >
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[a-z]*"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:element name="middleName" >
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[a-z]*"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:element name="lastName" >
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[a-z]*"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:element name="student" >
        <xs:complexType>
           <xs:sequence>
               <xs:element ref="firstName" />
               <xs:element ref="middleName" />
               <xs:element ref="lastName" />
           </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


student.xml
<?xml version="1.0" encoding="UTF-8"?>

<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="student.xsd">
    <firstName>hari</firstName>
    <middleName>krishna</middleName>
    <lastName>gurram</lastName>
</student>

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment