← Prev in month ← Prev in thread
Next in thread → Next in month →

XML Schema pattern facet quiz

From
Costello, Roger L. <>
To
"" <>
Date
2011-04-20T12:11:14Z
ID
<>
Thread
XML Schema pattern facet quiz
Hi Folks,

Here simpleType "A" is the base of simpleType "B":

    <xs:simpleType name="A">
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-z]{10}" />
        </xs:restriction>
    </xs:simpleType>
    
    <xs:simpleType name="B">
        <xs:restriction base="A">
            <xs:pattern value="[a-z]{20}" />
        </xs:restriction>
    </xs:simpleType>

Is that legal?

After all, clearly the pattern facet in "B" is not a restriction (subset) of the pattern facet in "A".

Further, how does it differ from this, where the two pattern facets are embedded in the same simpleType:

    <xs:simpleType name="C">
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-z]{10}" />
            <xs:pattern value="[a-z]{20}" />
        </xs:restriction>
    </xs:simpleType>

Is "C" equivalent to "A" plus "B"?

Scroll down to see the answers ...










































This is perfectly legal:

    <xs:simpleType name="A">
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-z]{10}" />
        </xs:restriction>
    </xs:simpleType>
    
    <xs:simpleType name="B">
        <xs:restriction base="A">
            <xs:pattern value="[a-z]{20}" />
        </xs:restriction>
    </xs:simpleType>

The pattern facet in "B" does not have to restrict/subclass the pattern facet in "A". In fact, the pattern facets are completely independent.

The pattern facets are "and-ed" together.  Consider this element, Test, declared to be of type "B":

  <xs:element name="Test" type="B" />

The value of "B" must consist of the letters a-z and the length must be exactly 10 characters AND exactly 20 characters. Obviously that is impossible, so Test has no valid value.

Compare the above two simpleTypes against this simpleType:

    <xs:simpleType name="C">
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-z]{10}" />
            <xs:pattern value="[a-z]{20}" />
        </xs:restriction>
    </xs:simpleType>

Pattern facets within a simpleType are "or-ed" together. Suppose the element, Test, is declared to be of type "C":

  <xs:element name="Test" type="C" />

The value of Test must consist of the letters a-z and the length must be exactly 10 characters OR exactly 20 characters. So either of these is valid:

    <Test>abcdefghijabcdefghij</Test>

    <Test>abcdefghij</Test>

Recap:

1. The pattern facets within a simpleType are "or-ed" together.

2. The pattern facets in a base simpleType and the sub-simpleType are "and-ed" together.

3. The pattern facets in a sub-simpleType do not have to restrict/subclass the patterns in the base simpleType.

/Roger
← Prev in month ← Prev in thread
Next in thread → Next in month →