14.3. Constraining Simple Content

Most of this chapter discusses the syntax of a regular expression. The link to XML schemas is that either a simple type or a complex type constrained to simple content may specify a regular expression. The value of a pattern constraining facet is a regular expression. Note that the pattern constraining facet applies only to certain base types (see the discussion on built-in datatypes in Chapter 12).

Caution

The interaction between two pattern constraining facets specified by the same simple type is different than the interaction between two pattern constraining facets when one is specified by the base type and one by the derived type. See this section and Section 14.3 for clarification.


The remainder of this chapter contains simple type examples, although the examples conceptually apply to complex types constrained to simple content. The simplest case is a simple type with a single pattern constraining facet. The other examples demonstrate the interactions of multiple pattern constraining facets, as well as the distinct behavior for derived types.

14.3.1. The pattern Constraining Facet

The XML representation of a simple type that has one pattern restriction is trivial. The following xmlRE constrains a corresponding value in an XML instance to a string that starts with “XML”:

<xsd:simpleType name="xmlRE"> 
    <xsd:restriction base="xsd:string"> 
        <xsd:pattern value="XML.*"/> 
    </xsd:restriction> 
</xsd:simpleType> 

In the pattern value, note that ‘.’ means any character and ‘*’ means unbounded occurrences. Therefore, the regular expression ‘XML.* ’ matches the string “XML” followed by any number of arbitrary characters. The following strings are all valid values in a corresponding XML instance:

  • XML

  • XML string

  • XML1234

  • XML is lame, at least without a corresponding XML schema

14.3.2. Multiple pattern Constraining Facets (or)

A simple type that has multiple pattern restrictions is not much more difficult: The patterns are “ored” together. In other words, an XML validator considers a corresponding value valid when the value matches the regular expression described by any one pattern. The following xmlSchemaRE constrains a corresponding value to a string that starts with “XMLor contains the string “schema”:

<xsd:simpleType name="xmlSchemaRE"> 
    <xsd:restriction base="xsd:string"> 
        <xsd:pattern value="XML.*"/> 
        <xsd:pattern value=".*schema.*"/> 
    </xsd:restriction> 
</xsd:simpleType> 

All of the following strings are valid values in a corresponding XML instance:

  • XML

  • schema

  • Write an XML schema in XML

  • XML is great

  • What about SQL schemas

  • XMLschema

  • schemaXML

  • XML schema document

  • XML is lame, at least without a corresponding XML schema

14.3.3. Constraining Derived Types (and)

Simple type derivations are slightly more complicated to conceptualize. The patterns of the derived type are “anded” together with the patterns in the base type; the corresponding value must validate against both sets of patterns concurrently. The following xmlSchemaDocumentRE constrains a value corresponding to the values specified in the previous section. In addition, the string must contain the string “document”:

<xsd:simpleType name="xmlSchemaDocumentRE"> 
    <xsd:restriction base="xmlSchemaRE"> 
        <xsd:pattern value=".*document.*"/> 
    </xsd:restriction> 
</xsd:simpleType> 

All of the following strings are valid values in a corresponding XML instance:

  • XMLdocument

  • schemadocument

  • documentSchema

  • XML schema document

  • Is there a document for SQL schemas?

Note that the corresponding value must contain “documentand either “XMLorschema”. Furthermore, if the value matches the ‘XML.*’ pattern, “XML” must appear at the beginning of the string.

The most confusing type of derivation occurs when both the base and derived types have multiple patterns. The following example derives xmlSchemaDocumentGreatRE, which has two patterns. The base xmlSchemaRE also has two patterns:

<xsd:simpleType name="xmlSchemaDocumentGreatRE"> 
    <xsd:restriction base="xmlSchemaRE"> 
        <xsd:pattern value=".*document.*"/> 
        <xsd:pattern value=".*great.*"/> 
    </xsd:restriction> 
</xsd:simpleType> 

Now the corresponding value must contain either “XMLorschemaand either “documentorgreat”. Note the emphasis in the previous sentence on ‘or’ and ‘and’, as the preceding example demonstrates simple types with multiple patterns as well as derivations of simple types. All the following strings are valid values in a corresponding XML instance:

  • XMLdocument

  • greatschema

  • schemagreat

  • XMLgreat

  • XML is great

  • An XML schema is great

  • XML can be in a document?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.94.173