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.
<!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)> |
<?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> |
<?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> |
<CONSTRAINT> <SELECTOR SELEXP="/reservations/room"/> <CC> @floor <= 12 </CC> <ACTION> <MESSAGE> The floor number <VALUE SELEXP="@floor"/> does not exist. </MESSAGE> </ACTION> </CONSTRAINT> |
<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> |
<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> |
<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> |
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 /> |
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> |
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> |
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> |
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> |
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> |
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> |