XML Schema Regular Expressions

Started by dhilipkumar, Mar 06, 2009, 03:09 PM

Previous topic - Next topic

dhilipkumar

XML Schema Regular Expressions

XML Schema regular expressions are used with the pattern facet to provide constraints or restrictions on a string of characters that conform to the defined pattern.

The pattern facet is a constraint on the value space of a data type, achieved by constraining the lexical space to literals that match a specific pattern. The value of pattern must be a regular expression.

Pattern provides for constraining a value space to values that are denoted by literals that match a specific regular expression.
Example

The following example defines a user-derived data type that creates a representation of postal codes in the United States, limited to strings that match a specific regular expression.
Input file: T.xml
Xml
Copy Code

<zips xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="t.xsd">
  <zip>12345</zip>
  <zip>12345-1234</zip>
</zips>

XML Schema: T.xsd
Xml

Copy Code

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <xs:element name="zips">
  <xs:complexType>
    <xs:sequence maxOccurs="unbounded">
      <xs:element name="zip" type="better-us-zipcode"></xs:element>
    </xs:sequence>
  </xs:complexType>
    </xs:element>
    <xs:simpleType name="better-us-zipcode">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{5}(-[0-9]{4})?"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>


There are two types of characters that are used in regular expressions: metacharacters and normal characters. A metacharacter is one of the following characters: ., \, ?, *, +, {, }, (, ), [, or ]. These characters have special meaning in regular expressions, but can be escaped to from atoms that denote the sets of strings containing only themselves. An escaped metacharacter has the behavior of a normal character.

A normal character is any XML character that is not a metacharacter. In regular expressions, a normal character is an atom that denotes the singleton set of strings containing only itself.

Source : MSDN

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview