The Reservations Example

Let us suppose we want to use an XML document to register the reservations of a certain hotel. To do that, we design a DTD which allows one or more rooms to be reported. Each room will have two sub-elements: a group in charge (gicharge, made up of two employees - picharge) and a set of events; and two attributes: floor - to indicate the location of the room, and id - to uniquely identify the room. Each events element has any number of event elements, each one referring to an event for which the room is booked. Each event has three sub-elements: date - made up of the beginning date and the final date, companies - the set of organizer companies, title - the title of the event in which any number of compn (company name) and tradem (trade-mark) elements may occur. Each company is identified by a name (compn), an organizer and a contact (compc).

After validating the structure of the document, at least four semantic constraints must be validated in order to have a completely valid document.

These constraints are: first - the value of every floor attribute descendant of the room elements must be 12 or less (or any other number that represents the number of floors the hotel has); second - for each event, the final date must occur after the beginning date; third - every contact must have a valid format (only numbers, exactly nine, and begin either with a 2, 91, 93 or 96 - in the portuguese situation); fourth - every compn element that occurs inside a certain title element must be of an organizer company. Notice that these four constraints correspond to the four categories of constraints enumerated in the Introduction.

Ficheiro com o DTD:

DTD:

<!ELEMENT reservations (room)+>
<!ELEMENT room (gicharge,events)>
<!ATTLIST room
    floor CDATA #REQUIRED
    id ID #REQUIRED>
<!ELEMENT gicharge (picharge,picharge)>
<!ELEMENT picharge (#PCDATA)>
<!ELEMENT events (event)*>
<!ELEMENT event (date,companies,title)>
<!ELEMENT date (dateb,datef)>
<!ELEMENT dateb (#PCDATA)>
<!ELEMENT datef (#PCDATA)>
<!ATTLIST dateb
    value CDATA #REQUIRED>
<!ATTLIST datef
    value CDATA #REQUIRED>
<!ELEMENT companies (company)+>
<!ELEMENT company (compn, organizer,compc)>
<!ELEMENT compn (#PCDATA)>
<!ELEMENT organizer (#PCDATA)>
<!ELEMENT compc (#PCDATA)>
<!ELEMENT title (#PCDATA|compn|tradem)*>
<!ELEMENT tradem (#PCDATA)>

Constraints:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CS>
    <!-- 1 -->
    <CONSTRAINT>
        <SELECTOR SELEXP="/reservations/room"/>
        <CC>
        @floor <= 12
    </CC>
        <ACTION>
            <MESSAGE>
            The floor number <VALUE SELEXP="@floor"/> does not exist.
        </MESSAGE>
        </ACTION>
    </CONSTRAINT>
    <!-- 2 -->
    <CONSTRAINT>
        <SELECTOR SELEXP="//room/events/event/date"/>
        <CC>
        dateb/@value <= datef/@value
    </CC>
        <ACTION>
            <MESSAGE>
            The final date: <VALUE SELEXP="datef"/> occurs before the beginning 
            date: <VALUE SELEXP="dateb"/> -this is not allowed.
        </MESSAGE>
        </ACTION>
    </CONSTRAINT>
    <!-- 3 -->
    <CONSTRAINT>
        <SELECTOR SELEXP="//compc"/>
        <CC>
        string-length(number(.)) = 9 and (substring(.,1,1)=2 or substring(.,1,2)=91 or
        substring(.,1,2)=93 or substring(.,1,2)=96)
    </CC>
        <ACTION>
            <MESSAGE>
            The contact for the company <VALUE SELEXP="../compn"/> is not a valid
            phone number.
        </MESSAGE>
        </ACTION>
    </CONSTRAINT>
    <!-- 4 -->
    <CONSTRAINT>
        <SELECTOR SELEXP="//title/compn"/>
        <LET NAME="keycompn" VALUE="."/>
        <CC>
    (count(../../companies/company[compn=$keycompn]) >= 1)
    </CC>
        <ACTION>
            <MESSAGE>
        The title of the event must not contain any company's name outside the set of
        organizer companies, as <VALUE SELEXP="."/> in a reservation of the room
        <VALUE SELEXP="../../../../@id"/>.
      </MESSAGE>
        </ACTION>
    </CONSTRAINT>
</CS>

Stylesheet generated:

<?xml version="1.0" encoding="utf-8" standalone="yes"?><!--
      Preprocessor for the XCSL Language
      http://www.di.uminho.pt/~jcr/PROJS/xcsl-www/
      Copyright (C) 2001 José Carlos Ramalho
      Permission to use granted under GPL or MPL.

      Version: 3.0
    -->
<my:stylesheet xmlns:my="http://www.w3.org/1999/XSL/Transform" 
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <my:output method="xml" omit-xml-declaration="no" encoding="iso-8859-1"
    standalone="yes" indent="yes"/>
   <my:template match="/">
      <doc-status>
         <my:apply-templates mode="constraint1"/>
         <my:apply-templates mode="constraint2"/>
         <my:apply-templates mode="constraint3"/>
         <my:apply-templates mode="constraint4"/>
      </doc-status>
   </my:template><!--
  
  .........................NEW CONSTRAINT.........................
  -->
   <my:template mode="constraint1" match="/reservations/room">
      <my:if test="not(
        @floor <= 12
    )">
         <err-message>
            The floor number <my:value-of select="@floor"/> does not exist.
        </err-message>
      </my:if>
   </my:template>
   <my:template match="text()" priority="-1" mode="constraint1"/><!--
  
  .........................NEW CONSTRAINT.........................
  -->
   <my:template mode="constraint2" match="//room/events/event/date">
      <my:if test="not(
        dateb/@value <= datef/@value
    )">
         <err-message>
            The final date: <my:value-of select="datef"/> occurs before the beginning
            date: <my:value-of select="dateb"/> -this is not allowed.
        </err-message>
      </my:if>
   </my:template>
   <my:template match="text()" priority="-1" mode="constraint2"/><!--
  
  .........................NEW CONSTRAINT.........................
  -->
   <my:template mode="constraint3" match="//compc">
      <my:if test="not(
        string-length(number(.)) = 9 and (substring(.,1,1)=2 or substring(.,1,2)=91 or
        substring(.,1,2)=93 or substring(.,1,2)=96)
    )">
         <err-message>
            The contact for the company <my:value-of select="../compn"/> is not a
            valid phone number.
        </err-message>
      </my:if>
   </my:template>
   <my:template match="text()" priority="-1" mode="constraint3"/><!--
  
  .........................NEW CONSTRAINT.........................
  -->
   <my:template mode="constraint4" match="//title/compn">
      <my:variable name="keycompn">
         <my:value-of select="."/>
      </my:variable>
      <my:if test="not(
    (count(../../companies/company[compn=$keycompn]) >= 1)
    )">
         <err-message>
        The title of the event must not contain any company's name outside the set 
        of organizer companies, as <my:value-of select="."/> in a reservation of the room
        <my:value-of select="../../../../@id"/>.
      </err-message>
      </my:if>
   </my:template>
   <my:template match="text()" priority="-1" mode="constraint4"/>
   <my:template match="text()" priority="-1"/>
</my:stylesheet>
    

The constraints one at a time:

The value of every floor attribute descendant of the room elements must be 12 or less.

Constraint 1:
<CONSTRAINT>
    <SELECTOR SELEXP="/reservations/room"/>
    <CC>
        @floor <= 12
    </CC>
    <ACTION>
        <MESSAGE>
        The floor number <VALUE SELEXP="@floor"/> does not exist.
    </MESSAGE>
    </ACTION>
</CONSTRAINT>
We check that the value of the floor attribute of each /reservations/room is less than or equal to 12. We set the context to /reservations/room analysing every occurence of this.
For each event, the final date must occur after the beginning date.

Constraint 2:
<CONSTRAINT>
    <SELECTOR SELEXP="//room/events/event/date"/>
    <CC>
        dateb/@value <= datef/@value
    </CC>
    <ACTION>
        <MESSAGE>
            The final date: <VALUE SELEXP="datef"/> occurs before the beginning date:
            <VALUE SELEXP="dateb"/> -this is not allowed.
        </MESSAGE>
    </ACTION>
</CONSTRAINT>
We set the context to //room/events/event/date, to analyse every occurence of date. Then we check if dateb is previous to datef.
Every contact must have a valid format - only numbers, in number of nine, and begin either with a 2, 91, 93 or 96.

Constraint 3:
<CONSTRAINT>
    <SELECTOR SELEXP="//compc"/>
    <CC>
        string-length(number(.)) = 9 and (substring(.,1,1)=2 or substring(.,1,2)=91 or
        substring(.,1,2)=93 or substring(.,1,2)=96)
    </CC>
    <ACTION>
        <MESSAGE>
            The contact for the company <VALUE SELEXP="../compn"/> is not a valid phone 
            number.
        </MESSAGE>
    </ACTION>
</CONSTRAINT>
The context is now set to //compc. We verify that the contact is only made up of digits by checking that the length of the number obtained after the application of the number function -which returns only the digits, for instance number(232s)=232- is 9. Afterwards we check if the beginning substring of the contact is either equal to 2, 91, 93 or 96. Finally we make the interception of these two boolean values.
Every compn element that occurs inside a certain title element must be of an organizer company.

Constraint 4:
<CONSTRAINT>
    <SELECTOR SELEXP="//title/compn"/>
    <LET NAME="keycompn" VALUE="."/>
    <CC>
        (count(../../companies/company[compn=$keycompn]) >= 1)
    </CC>
    <ACTION>
         <MESSAGE>
            The title of the event must not contain any company's name outside 
            the set of organizer companies, as <VALUE SELEXP="."/> in a reservation of the 
            room <VALUE SELEXP="../../../../@id"/>.
         </MESSAGE>
    </ACTION>
</CONSTRAINT>
We use a LET element to place in keycompn the list of values that the compn element assumes in the //title/compn context. Later, the number of occurrences of each instance of compn (in companies/company descendant of the current's title event) in the keycompn list is counted and compared with 1. The action will be triggered whenever the negation of "greater than or equal" occurs, i.e., the name does not occur as an organizer of the event.

Applications:

Application 1:

Documents where the value of every floor attribute descendant of the room elements is 12 or less; for each event, the final date occurs after the beginning date; every contact has a valid format; and every compn element that occurs inside a certain title element refers an organizer company. The result was a document with no errors.

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="0" id="s1">
        <gicharge>
            <picharge>Ana</picharge>
            <picharge>Manuel</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010515">15th May 2001</dateb>
                    <datef value="20010515">15th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>Candle</compn>
                        <organizer>Gabriel</organizer>
                        <compc>214848737</compc>
                    </company>
                </companies>
                <title>
                    <compn>Candle</compn>Net</title>
            </event>
            <event>
                <date>
                    <dateb value="20010626">26th June 2001</dateb>
                    <datef value="20010626">26th June 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>EMC</compn>
                        <organizer>Flavio</organizer>
                        <compc>219458372</compc>
                    </company>
                </companies>
                <title>
                    <compn>EMC</compn> developments for <tradem>OS/390</tradem>
                </title>
            </event>
        </events>
    </room>
    <room floor="1" id="s2">
        <gicharge>
            <picharge>Ana</picharge>
            <picharge>Manuel</picharge>
        </gicharge>
        <events/>
    </room>
    <room floor="1" id="s3">
        <gicharge>
            <picharge>José</picharge>
            <picharge>Rita</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010524">24th May 2001</dateb>
                    <datef value="20010525">25th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>SOL-S</compn>
                        <organizer>Maria</organizer>
                        <compc>213487659</compc>
                    </company>
                    <company>
                        <compn>CheckPoint</compn>
                        <organizer>Carlos</organizer>
                        <compc>224357985</compc>
                    </company>
                    <company>
                        <compn>Remedy</compn>
                        <organizer>Bruno</organizer>
                        <compc>218705464</compc>
                    </company>
                </companies>
                <title>EBusiness2000 - <compn>CheckPoint</compn> and <compn>Remedy</compn>
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

  <?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?> 
  <doc-status /> 
Application 2:

Documents where the value of one or more floor attributes descendant of the room elements is greater than 12; for each event, the final date occurs after the beginning date; every contact has a valid format; and every compn element that occurs inside a certain title element refers to an organizer company. For instance if we have two room elements and one is said to be located in the 14th floor, the result will be a document showing the error:

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="14" id="s1">
        <gicharge>
            <picharge>Ana</picharge>
            <picharge>Manuel</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010626">26th June 2001</dateb>
                    <datef value="20010626">26th June 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>EMC</compn>
                        <organizer>Flavio</organizer>
                        <compc>219458372</compc>
                    </company>
                </companies>
                <title>
                    <compn>EMC</compn> developments
                </title>
            </event>
        </events>
    </room>
    <room floor="1" id="s3">
        <gicharge>
            <picharge>José</picharge>
            <picharge>Bonifácio</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010524">24th May 2001</dateb>
                    <datef value="20010525">25th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>Remedy</compn>
                        <organizer>Bruno</organizer>
                        <compc>218705464</compc>
                    </company>
                </companies>
                <title>EBusiness2000 - <compn>Remedy</compn> products
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<doc-status>
    <err-message>
            The floor number 14 does not exist.
        </err-message>
</doc-status>
Application 3:

Documents where the value of every floor attribute descendant of the room elements is 12 or less; for one or more events, the final date occurs before the beginning date; every contact has a valid format; and every compn element that occurs inside a certain title element refers an organizer company. For instance if we have two event elements and both have wrong dates, the result will be a document showing the errors.

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="0" id="s1">
        <gicharge>
            <picharge>Ana</picharge>
            <picharge>Manuel</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010515">15th May 2001</dateb>
                    <datef value="20010513">13th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>Promosoft</compn>
                        <organizer>Carlos</organizer>
                        <compc>219878586</compc>
                    </company>
                </companies>
                <title>
                    Portal/SME</title>
            </event>
            <event>
                <date>
                    <dateb value="20010626">26th June 2001</dateb>
                    <datef value="20010625">25th June 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>EMC</compn>
                        <organizer>Flavio</organizer>
                        <compc>219458372</compc>
                    </company>
                </companies>
                <title>
                    <tradem>OS/390</tradem>
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<doc-status>
    <err-message>
            The final date: 13th May 2001 occurs before the beginning date:
            15th May 2001 -this is not allowed.
        </err-message>
    <err-message>
            The final date: 25th June 2001 occurs before the beginning date:
            26th June 2001 -this is not allowed.
        </err-message>
</doc-status>
Application 4:

Documents where the value of every floor attribute descendant of the room elements is 12 or less; for each event, the final date occurs after the beginning date; one or more contacts have an invalid format; and every compn element that occurs inside a certain title element refers an organizer company. For instance if we have three company elements and two of them have an invalid format, the result will be a document showing the errors.

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="1" id="s3">
        <gicharge>
            <picharge>José</picharge>
            <picharge>Bonifácio</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010524">24th May 2001</dateb>
                    <datef value="20010525">25th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>SOL-S</compn>
                        <organizer>Maria</organizer>
                        <compc>413487659</compc>
                    </company>
                    <company>
                        <compn>CheckPoint</compn>
                        <organizer>Carlos</organizer>
                        <compc>224357985</compc>
                    </company>
                    <company>
                        <compn>Remedy</compn>
                        <organizer>Bruno</organizer>
                        <compc>818705464</compc>
                    </company>
                </companies>
                <title>EBusiness2000 - <compn>CheckPoint</compn> 
                and <compn>Remedy</compn>
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<doc-status>
    <err-message>
            The contact for the company SOL-S is not a valid phone number.
        </err-message>
    <err-message>
            The contact for the company Remedy is not a valid phone number.
        </err-message>
</doc-status>
Application 5:

Documents where the value of every floor attribute descendant of the room elements is 12 or less; for each event, the final date occurs after the beginning date; every contact has a valid format; and one or more compn elements occuring inside a certain title element refer to a non-organizer company. For instance if we have two events, in the first one's title the only compn that occurs refers to a non-organizer company, and in the second one's title the second and third compn that occur refer to non-organizer companies, the result will be a document showing the errors.

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="0" id="s1">
        <gicharge>
            <picharge>Ana</picharge>
            <picharge>Manuel</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010626">26th June 2001</dateb>
                    <datef value="20010626">26th June 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>EMC</compn>
                        <organizer>Flavio</organizer>
                        <compc>219458372</compc>
                    </company>
                </companies>
                <title>
                    <compn>EMC3</compn> developments
                </title>
            </event>
        </events>
    </room>
    <room floor="1" id="s3">
        <gicharge>
            <picharge>José</picharge>
            <picharge>Bonifácio</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010524">24th May 2001</dateb>
                    <datef value="20010525">25th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>SOL-S</compn>
                        <organizer>Coimbra</organizer>
                        <compc>213487659</compc>
                    </company>
                    <company>
                        <compn>CheckPoint</compn>
                        <organizer>Esteves</organizer>
                        <compc>224357985</compc>
                    </company>
                    <company>
                        <compn>Remedy</compn>
                        <organizer>Botas</organizer>
                        <compc>218705464</compc>
                    </company>
                </companies>
                <title>EBusiness2000 - <compn>CheckPoint</compn> and 
                <compn>RemedyA</compn> and <compn>CA</compn>
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<doc-status>
    <err-message>
        The title of the event must not contain any company's name outside 
        the set of organizer companies, as EMC3 in a reservation of the room
        s1.
      </err-message>
    <err-message>
        The title of the event must not contain any company's name outside the 
        set of organizer companies, as RemedyA in a reservation of the room
        s3.
      </err-message>
    <err-message>
        The title of the event must not contain any company's name outside the 
        set of organizer companies, as CA in a reservation of the room
        s3.
      </err-message>
</doc-status>
Application 6:

Documents where a combination of the previous four situations occur, i.e, one or more conditions are violated. For instance if we have one event, the final date occurs before the beginning date, one contact does not have the allowed form and two of the compn elements which occur in the title are of non-organizer companies, the result will be a document showing the errors.

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="1" id="s3">
        <gicharge>
            <picharge>José</picharge>
            <picharge>Bonifácio</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010524">24th May 2001</dateb>
                    <datef value="20010522">22th May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>CheckPoint</compn>
                        <organizer>Carlos</organizer>
                        <compc>824357985</compc>
                    </company>
                    <company>
                        <compn>Remedy</compn>
                        <organizer>Bruno</organizer>
                        <compc>218705464</compc>
                    </company>
                </companies>
                <title>EBusiness2000 - <compn>CheckPoint</compn> and 
                <compn>RemedyA</compn> and <compn>CA</compn>
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<doc-status>
    <err-message>
            The final date: 22th May 2001 occurs before the beginning date:
            24th May 2001 -this is not allowed.
        </err-message>
    <err-message>
            The contact for the company CheckPoint is not a valid phone number.
        </err-message>
    <err-message>
        The title of the event must not contain any company's name outside the set 
        of organizer companies, as RemedyA in a reservation of the room
        s3.
      </err-message>
    <err-message>
        The title of the event must not contain any company's name outside the set 
        of organizer companies, as CA in a reservation of the room
        s3.
      </err-message>
</doc-status>
Application 7:

Documents where simultaneously, the value of one or more floor attributes descendant of the room elements is greater than 12; for one or more events, the final date occurs before the beginning date; one or more contacts have an invalid format; and one or more compn elements occuring inside a certain title element refer to a non-organizer company. For instance if we have one room indicated to be in the 14th floor, where occurs only one event with the final date before the beginning date, one contact with a not allowed form and one compn element occuring in the title being of a non-organizer company, the result will be a document showing the errors.

XML instance:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE reservations SYSTEM "reserv.dtd">
<reservations>
    <room floor="17" id="s3">
        <gicharge>
            <picharge>José</picharge>
            <picharge>Rita</picharge>
        </gicharge>
        <events>
            <event>
                <date>
                    <dateb value="20010524">24th May 2001</dateb>
                    <datef value="20010522">22nd May 2001</datef>
                </date>
                <companies>
                    <company>
                        <compn>CheckPoint</compn>
                        <organizer>Esteves</organizer>
                        <compc>824357985</compc>
                    </company>
                </companies>
                <title>EBusiness2000 - <compn>CheckPoint</compn> and 
                <compn>CA</compn>
                </title>
            </event>
        </events>
    </room>
</reservations>

The generated file:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<doc-status>
    <err-message>
            The floor number 17 does not exist.
        </err-message>
    <err-message>
            The final date: 22nd May 2001 occurs before the beginning date:
            24th May 2001 -this is not allowed.
        </err-message>
    <err-message>
            The contact for the company CheckPoint is not a valid phone number.
        </err-message>
    <err-message>
        The title of the event must not contain any company's name outside the
        set of organizer companies, as CA in a reservation of the room
        s3.
      </err-message>
</doc-status>

Back