zed13
zed13

Reputation: 387

XSD : fail to compile custom type

I try to create an xsd schame for this xml file :

<?xml version="1.0"?>
<Listelocataires xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="locataires.xsd">
    <NoImmeuble>221B</NoImmeuble>
        <etage n="1">
            <appartement cote="gauche">
                <nom>Watson</nom>
            </appartement>
            <appartement cote="droit">
                <nom>Holmes</nom>
                <telephone>020 7465 4321></telephone>
            </appartement>
        </etage>
        <etage n="0">
            <appartement cote="droit">
                <nom>Hudson</nom>
                <telephone>020 7465 4321></telephone>
            </appartement>
        </etage>
</Listelocataires>

I try to create an custom type for the validation of the telephone number, but it didn't accept my answer, here is my xsd schema :

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Listelocataires">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="NoImmeuble" minOccurs="1" maxOccurs="unbounded" />
                <xsd:element ref="etage" minOccurs="1" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="NoImmeuble" type="xsd:string" />

    <xsd:element name="etage">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="appartement" minOccurs="1" maxOccurs="unbounded" />
            </xsd:sequence>
            <xsd:attribute name="n" type="xsd:string" use="required" /> 
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="appartement">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="nom" minOccurs="1" maxOccurs="unbounded" />
                <xsd:element ref="telephone" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
            <xsd:attribute name="cote" type="xsd:string" /> 
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="nom" type="xsd:string" />
    <xsd:element name="telephone" type="typeTelephone"/>

    <xsd:simpleType name="typeTelephone">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="\d{3} \d{4} \d{4}" />
        </xsd:restriction>
    </xsd:simpleType>

Xmllint give me those errors :

locataires.xml:10: element telephone: Schemas validity error : Element 'telephone': [facet 'pattern'] The value 'à020 7465 4321>' is not accepted by the pattern 'à\d{3} \d{4} \d{4}'. locataires.xml:10: element telephone: Schemas validity error : Element 'telephone': 'à020 7465 4321>' is not a valid value of the atomic type 'typeTelephone'. locataires.xml:16: element telephone: Schemas validity error : Element 'telephone': [facet 'pattern'] The value 'à020 7465 4321>' is not accepted by the pattern 'à\d{3} \d{4} \d{4}'. locataires.xml:16: element telephone: Schemas validity error : Element 'telephone': 'à020 7465 4321>' is not a valid value of the atomic type 'typeTelephone'.

I don't understand why, if you see, please help me, Best regards, Zed13

Upvotes: 1

Views: 63

Answers (1)

kjhughes
kjhughes

Reputation: 111521

The regex for telephone does not permit the trailing > characters that you have in your data.

To validate your XML successfully, change the following line from

<telephone>020 7465 4321></telephone>

to

<telephone>020 7465 4321</telephone>

And this one from

<telephone>020 7465 4321></telephone>

to

<telephone>020 7465 4321</telephone>

Or, change the XSD regex from

        <xsd:pattern value="\d{3} \d{4} \d{4}" />

to

        <xsd:pattern value="\d{3} \d{4} \d{4}>" />

Upvotes: 1

Related Questions