Question

Here is a simplified XML Schema example I have written:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://pius.lt" xmlns="http://pius.lt" elementFormDefault="qualified">
    <xs:complexType name="person">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="email" type="xs:string"/>
            <xs:element name="sex" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="user">
        <xs:complexContent>
            <xs:extension base="person">
                <xs:sequence>
                    <xs:element name="password" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="id" type="xs:integer" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="admin">
        <xs:complexContent>
            <xs:restriction base="user">
                <xs:sequence>
                    <xs:element name="email" type="xs:string" fixed="admin@pius.lt"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

Error I get is:

Not valid.
Error - Line 22, 31: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 31; rcase-Recurse.2: There is not a complete functional mapping between the particles.
Error - Line 22, 31: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 31; derivation-ok-restriction.5.4.2: Error for type 'admin'.  The particle of the type is not a valid restriction of the particle of the base.

What am I missing?

P.S. Is it possible to apply pattern restriction on that "email" field? Something like .+@example.com.

Was it helpful?

Solution

When you derive by restriction you need to provide the complete content model in the subtype - anything you don't say is allowed in the subtype is assumed to be forbidden.

So the way your schema is written the admin type would only be allowed to contain an email element and nothing else, but this is not a valid restriction of user because an element of type user must have exactly one name, then one email, then one sex, then one password element as children.

You're falling foul of rule 2.2 under http://www.w3.org/TR/xmlschema-1/#rcase-Recurse:

All particles in the {particles} of B which are not mapped to by any particle in the {particles} of R are ·emptiable· as defined by Particle Emptiable (§3.9.6).

i.e. anything you want to leave out in the restricted type must have been optional in the parent to start with - any instance of the restricted type must also be a valid instance of the parent type from which it is restricted.

If you genuinely do want the admin type to contain only the email element then you'd have to mark the other elements of person and user with minOccurs="0", but I suspect that really you want

<xs:complexType name="admin">
    <xs:complexContent>
        <xs:restriction base="user">
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="email" type="xs:string" fixed="admin@pius.lt"/>
                <xs:element name="sex" type="xs:string"/>
                <xs:element name="password" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:integer" use="required"/>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top