Search code examples
xmldtd

XML Element is not matching the DTD


Consider the following snippet -

DTD

<!DOCTYPE hotels_list [
    <!ELEMENT hotels_list ( hotel+ ) >
    <!ELEMENT hotel ( id, name, stars, Facilities, Address, distance_from_center, available ) >
    <!ELEMENT id ( #PCDATA ) >
    <!ELEMENT name ( #PCDATA ) >
    <!ELEMENT stars ( #PCDATA ) >
    <!ELEMENT Address ( #PCDATA ) >
    <!-- Facilities only contains at least one of the options in (internet,gym,restaurant,parking,pickup -->
    <!ELEMENT Facilities (Internet*,Gym*,Restaurant*,Parking*,Pickup*) >
    <!ELEMENT available ( #PCDATA ) >
    <!ELEMENT distance_from_center ( #PCDATA ) >
    ]>

XML Code

<hotels_list>
    <hotel>
        <id>1</id>
        <name>Les Jardins Du Marais</name>
        <stars>3</stars>
        <Facilities>Internet</Facilities>
        <Address>74 rue Amelot, Paris, 75011</Address>
        <distance_from_center>2</distance_from_center>
        <available>True</available>
    </hotel>
    <hotel>
        <id>2</id>
        <name>Golden Tulip Little Palace</name>
        <stars>4</stars>
        <Facilities>Internet,Gym,Parking,Restaurant</Facilities>
        <Address>4 rue Salomon
        De Caus, Paris 75003</Address>
        <distance_from_center>0.1</distance_from_center>
        <available>False</available>
    </hotel>
</hotels_list>

But the facilities shows error as

The content of element type "Facilities" must match "(Internet*,Gym*,Restaurant*,Parking*,Pickup*)".xml(MSG_CONTENT_INVALID)

Am I missing something here , can someone please help?

thank you for your help.


Solution

  • Facilities is a list of elements and not a regular expression for text content.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hotels_list [
        <!ELEMENT hotels_list (hotel+)>
        <!ELEMENT hotel (id, name, stars, Facilities, Address, distance_from_center, available)>
        <!ELEMENT id (#PCDATA)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT stars (#PCDATA)>
        <!ELEMENT Address (#PCDATA)>
        <!-- Facilities only contains at least one of the options in (internet,gym,restaurant,parking,pickup -->
        <!ELEMENT Facilities (Internet*, Gym*, Restaurant*, Parking*, Pickup*)>
        <!ELEMENT Internet (#PCDATA)>
        <!ELEMENT Gym (#PCDATA)>
        <!ELEMENT Restaurant (#PCDATA)>
        <!ELEMENT Parking (#PCDATA)>
        <!ELEMENT Pickup (#PCDATA)>
        <!ELEMENT available (#PCDATA)>
        <!ELEMENT distance_from_center (#PCDATA)>
    ]>
    <hotels_list>
        <hotel>
            <id>1</id>
            <name>Les Jardins Du Marais</name>
            <stars>3</stars>
            <Facilities><Internet/></Facilities>
            <Address>74 rue Amelot, Paris, 75011</Address>
            <distance_from_center>2</distance_from_center>
            <available>True</available>
        </hotel>
        <hotel>
            <id>2</id>
            <name>Golden Tulip Little Palace</name>
            <stars>4</stars>
            <Facilities><Internet/><Gym/><Restaurant/><Parking/></Facilities>
            <Address>4 rue Salomon
            De Caus, Paris 75003</Address>
            <distance_from_center>0.1</distance_from_center>
            <available>False</available>
        </hotel>
    </hotels_list>