Вы находитесь на странице: 1из 3

Create a DTD for a catalog of four-stroke

motorbikes, where each bike has the following


child elements
make,model,year,color,engine,chasis number and
accessories.The engine element has the child
elements engine number, number of
cylinders,type of fuel. The accessories element
has the attributes like disc brake,auto start &
radio , each of which is required and has the
possible values yes and no . Entities must be
declared for the names of the popular motorbike
makes.
The DTD is automob.dtd
<!ELEMENT Catalog (MotorBike)*>
<!ELEMENT MotorBike(make,model,year,color,engine,chasis-num,accessories)>
<!ELEMENT make(#PCDATA)>
<!ELEMENT model(#PCDATA)>
<!ELEMENT year(#PCDATA)>
<!ELEMENT color (#PCDATA)>
<!ELEMENT engine(engine-number,cylinders-number,fuel-type)
<!ELEMENT chasis-number (#PCDATA)>
<!ELEMENT accessories (#PCDATA)>
<!ATTLIST accessories disc-brake(yes/no) # REQUIRED
Auto-start(yes/no) #REQUIRED
Radio(yes/no) #REQUIRED>
The DTD can be used by the XML document as shown below
Automobdemo.xml
<?xml version=1.0?>

<catalog>
<MotorBike>
<make> Bajaj </make>
<model> Pulsar 180 </model>
<year>3</year>
<color>Black</color>
<engine>
<engine-number>456</engine-number>
<cylinders-number>20</cylinders-number>
<fuel-type> premium</fuel-type>
</engine>
<chasis-number>34324</chasis-number>
<accessories disc-brake=yes auto-start=yes radio=no/>
<motorbike>
</catalog>
Differences between SAX and DOM parsers

SAX
1)import javax.xml.parsers.*;
Import org.xml.sax.*;
Import
org.xml.sax.helpers.*;
2)Parses node by node

DOM
1)import javax.xml.parsers.*;
Import org.w3c.dom.*;
2)Stores the entire XML
document into memory
before processing
3)Occupies more memory

3)Doesnt store the XML in


memory
4)We cannot insert or delete a 4)We can insert or delete a
node
node
5)Top to bottom traversing
5)Traverse in any direction
6)SAX is an event based
6)DOM is a tree model parser
parser

7)SAX doesnt preserve


comments
8)SAX parser serves the client
application always only with
pieces of the document

7)DOM preserves comments


8)DOM parser always serves
the client application with the
entire document.

Cleary explain the advantages of XML schemas over DTDs


The advantages of XML schemas are as follows:
i.

ii.

iii.

iv.

XML schema use basic XML syntax XML schemas are created by using
XML syntax whereas DTDs use separate syntax. The creation and learning
time of DTD syntax is more compared to XML schema .Some XML
technologies such as Extensible Stylesheet Language
Transformations(XSLT ) are used with XML schemas, whereas XSLTs are not
allowed to work with DTDs.
XML schema support namespace XML schemas support namespace
functionality, but DTDs doesnt support this functionality completely.XML
schema helps in defining the vocabularies that use namespace
declarations. They also allow the usage of multiple namespaces in XML
documents with less rigidity. For example, while designing an XML schema
the prefixes of namespace are not required since the end-user to decide it.
But, in DTDs the namespace prefixes are essential to be specified.
XML schema allow the validation of text elements based on
datatypes XML schemas specify the type of textual data that can be used
within attributes and elements. This is done by the simple type
declarations .For example, by the usage of these types , one can specify
that an element may include only data values, positive number or certain
range of numbers. Hence XML schemas can control the documents more
rigidly. The most important feature of XML schemas is that , it include the
commonly used simple types
XML schema allows the creation of complex and reusable content
models easily In a DTD , content model can be reused only when the
utilization of parameter entities is allowed . But , this may lead to some
situations where the parts of DTD are difficult to be reusable. XML
schemas provide a wide variety of mechanisms to reuse the content
models and also model some complex programming concepts easily.

Вам также может понравиться