Udostępnij za pośrednictwem


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

<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 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.

The following topics describe the XML Schema regular expressions.

To understand the relationships in XML Schema Regular Expressions, see the XML Schema Regular Expressions Reference Chart.

See Also

Reference

XML Schemas (XSD) Reference
XML Schema Regular Expressions Reference Chart
Atom

Concepts

Data Type Facets