You are on page 1of 12

Pilar Alonso Suela

1ST DAMEL
Task 4
Markup language

DTD
AND
XML
SCH
EMA
1

Pilar Alonso Suela


Trademark Language Task 4
1st Damel

1 A company uses XML files to inventory, among other things, printers. For each
printer you want to save its serial number, brand, model, weight, paper sizes it can work
with, the cartridge it uses, the type, the year of purchase and whether it is networked or
not.

An example of these files would be:

<printers>
<printer numSerie="i245" type="laser" purchase="2010">
<brand>Epson</brand>.
<model>EPL300</model>.
<weight>4.52</weight>.
<size>A4</size>.
<size>A5</size>.
</cartridge>C-123BV</cartridge>.
<network/>
</printer>.
<printer numSerie="i246" type="dot matrix">
<brand>HP</brand>.
<model>LaserJet 2410</model>.
<weight>3.2</weight>.
<size>A4</size>.
</cartridge>C-456P</cartridge>.
</printer>.
</printers>
Write an XML Schema for these files, choosing the most appropriate data types and
taking into account that:

• The weight is a positive number and cannot have more than two decimal places.
• There may be more than one size.
• The type attribute can only take the values 'matrix', 'laser' and 'ink'. It is
mandatory.
• The numSeries attribute is mandatory and acts as an identifier.
• The cartridge consists of a capital C, a hyphen, three numbers and one or two
capital letters.
• All elements are mandatory, except enred.
• The enred element is optional. If present, it is an empty element.
• The purchase attribute, optional, stores the year of purchase. It is a positive
integer.
• Each file contains data for one or more printers.
2

XML SCHEMA

The parent element is <xs:schema> and must contain declarations for all elements
found in the xml.

The child element is <xs:element>.

In xml schema we have no way to define the root element in a special way, we
will indicate it with <xs:element name="root"> , which we will define as a
complex type if it has more elements inside and the xs:sequence tag to add the
different elements that form it and its restrictions. This way we will have a nested
xml schema.

Attributes must be declared after the sequence tag that defines the root elements.

The XML schema will allow us to perform advanced validations.

This is the XMl validated against document1.xsd:

<?xml version="1.0" encoding="UTF-8"?>

<printer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Document1.xsd"> <!--link with relative path
to validate against the xsd-->
3

<printer numSerie="i245" type="laser" purchase="2010">

<brand>Epson</brand>.

<model>EPL300</model>.

<weight>4.52</weight>.

<size>A4</size>.

<size>A5</size>.

</cartridge>C-123BV</cartridge>.

<network/>

</printer>.

<printer numSerie="i246" type="dot matrix">

<brand>HP</brand>.

<model>LaserJet 2410</model>.

<weight>3.2</weight>.

<size>A4</size>.

</cartridge>C-456P</cartridge>.

</printer>.

</printers>

Now the XML Schema called document1:


4

I have added comments like this:<!-------------> although comments made in this


way are preferable as they are actionable and can be structured:

<xs:annotation>
<xs:documentation>
Define a list of printers.Pilar Alonso Suela.Task 04.1 DAMEL
</xs:documentation>
</xs:annotation>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xs:element name="printers">

<xs:complexType>

<xs:sequence>

<!--here we reference the printer element, the references are a way to make
the xml schema more readable.there will be data for one or more printers-->

<xs:element ref="printer" minOccurs="1"


maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>
5

</xs:element>

<xs:element name="printer">

<xs:complexType>

<xs:sequence>

<xs:element name="brand" type="xs:string" minOccurs="1"


maxOccurs="unbounded"/>

<xs:element name="model" type="xs:string" minOccurs="1"


maxOccurs="unbounded"/>

<xs:element name="weight" minOccurs="1" maxOccurs="unbounded">

<!--the weight must be a positive number with no more than two decimal
places and must be declared within xs:restriction-->

<xs:simpleType> <!--we define the simple type value-->.

<xs:restriction base="xs:decimal"><!--base indicates the type of data from


which the restriction is made, in this case it must be decimal-->>.

<xs:fractionDigits value="2"/> <!--I use fractionDigits to specify the


number of decimal places, since total digits although it can be applied in decimals
specifies the total number of digits--> <!--I use fractionDigits to specify the
number of decimal places, since total digits although it can be applied in decimals
specifies the total number of digits-->.

<xs:minInclusive value="1"/>

</xs:restriction>

</xs:simpleType>

</xs:element> <!--there can be more than one size-->.

<xs:element name="size" type="xs:string" minOccurs="1"


maxOccurs="unbounded"/><!--we define an unlimited maximum number of
sizes, with a minimum occurrence of 1-->

<xs:element name="cartridge" minOccurs="1" maxOccurs="unbounded" >

<xs:simpleType>

<xs:restriction base="xs:string">
6

<xs:pattern value="[C][\-][0-9][0-9][0-9][A-Z]{1,2}"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name="enred" type="xs:string" default="" minOccurs="0" />

</xs:sequence>

<!--attributes -->

<!--the serial number attribute must be mandatory and also serve as the
printer identifier-->>

<xs:attribute name="numSeries" use="required">

<xs:simpleType>

<xs:restriction base="xs:ID"><!--Let's set the ID type restriction-->

</xs:restriction>

</xs:simpleType>

</xs:attribute>

<!--The type attribute must be mandatory and must only contain the values: dot
matrix, laser and ink-->.

<xs:attribute name="type" use="required">

<xs:simpleType><!--we define the simple type-->.

<xs:restriction base="xs:string"><!--the type of data to which the restriction is


applied-->

<xs:enumeration value="matrix"/> <!--inside an enumeration we will put the


different values-->.

<xs:enumeration value="laser"/>

<xs:enumeration value="ink"/>

</xs:restriction>
7

</xs:simpleType>

</xs:attribute>

<xs:attribute name="purchase" type="xs:positiveInteger" use="optional"/><!--


the attribute purchase is optional and is a positive integer, by default if nothing is
specified the attribute will be optional, although I preferred to specify it-->.

</xs:complexType>

</xs:element>

</xs:schema>

2.- An association of mus players uses XML files to store the information of the
tournaments it organizes. For each tournament you want to save the year it was held, the
previous winner (its id) and the participants' information. We would like to store the
participant's identifier, his/her partner's identifier, his/her full name, age, country and
also whether he/she is a seed or not.

An example of these files would be:

<tournament edition="1998" previousWinner="j01">


<participant idP="j01" couple="j02">
<first name>Manuel Pérez</first name>.
<age>23</age>.
<country> Spain</country>.

<serialHead/>
</participant>.
<participant idP="j02" couple="j01">
<first name>Manuel Gómez</first name>.
<age>25</age>.
<country>Spain</country>.
</participant>.
<participant idP="j03" couple="j04">
<first name>Ana Puertas</first name>.
<age>22</age>.
<country> E5spain</country>.
<serialHead/>
</participant>.
<participant idP="j04" couple="j03">
<first name>Paco Fraile</first name>.
<age>45</age>.
<country>Spain</country>.
</participant>.
</tournament>.
Write an appropriate DTD for these files, keeping in mind that:
8

• All attributes are mandatory.


• The idP attribute acts as an identifier.
• The partner attribute is the identifier of another participant that must be present
in the file.
• The element headOfSeries is optional, the others are mandatory.
• Each file contains data for only one tournament.
• The tournament must have participants.

DTD

A DTD is a document with a .dtd extension and is responsible for ensuring that the XML
data complies with the constraints imposed in the dtd.

In this case I have declared it internally in the xml , but it is also possible to declare it
externally in the form of a plain text document with extension .dtd

Terminal elements (do not contain elements) are declared as follows: <!ELEMENT
data_type_name>.

Non-terminal elements in this other way: <!ELEMENT A(B,C)>

Of which it is necessary to define their cardinality: ?(option),*(0,1,several),+(one or


several)|(operator of choice)

<!ATTLIST to declare attributes and there are several


types:ID,IDREF,CDATA,MNTOKEN and the modifiers:
IMPLIED,FIXED,REQUIRED,literal.

We will also create entities to work with constant values.

TASK REQUIREMENTS

As the tournament must have participants we use > + to set a minimum of 1 or more in
the cardinality of the tournament element.

As the attributes must be mandatory we will use #REQUIRED

Within the participant element we will find that all contained elements must have at least
one occurrence (+) and that the element headOfSeries is optional so I use >?

Since idP must be an identifier >we will add ID to declare the attribute as an identifier.
9

The partner attribute must refer to the idP of another participant for this I will use
IDREF.

It would look like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tournament [

<!ELEMENT tournament (participant+)>

<!ATTLIST tournament CDATA edition #REQUIRED>

<!ATTLIST previous tournamentWINNER CDATA #REQUIRED>

<!ELEMENT participant (name+,age+,country+,headOfSeries?)>

<!ATTLIST participant idP ID #REQUIRED>

<!ATTLIST participant IDREF partner #REQUIRED>.

<!ELEMENT name (#PCDATA)>

<!ELEMENT age (#PCDATA)>

<!ELEMENT country (#PCDATA)>

<!ELEMENT headOfSeries (#PCDATA)>

]>

<tournament edition="1998" previousWinner="j01">

<participant idP="j01" couple="j02">

<first name>Manuel Pérez</first name>.

<age>23</age>.

<country> Spain</country>.

<serialHead/>

</participant>.

<participant idP="j02" couple="j01">


1
0
<first name>Manuel Gómez</first name>.

<age>25</age>.

<country>Spain</country>.

</participant>.

<participant idP="j03" couple="j04">

<first name>Ana Puertas</first name>.

<age>22</age>.

<country> E5spain</country>.

<serialHead/>

</participant>.

<participant idP="j04" couple="j03">

<first name>Paco Fraile</first name>.

<age>45</age>.

<country>Spain</country>.

</participant>.

</tournament>.

Attached is a screenshot of Copy Editor and the file will be .dtd:


1
1

You might also like