You are on page 1of 44

UNIT-IV

INTRODUCTION TO XML
 XML or eXtensible Markup Language is a text based markup language that
enables you to store data in a structured format.
 XML is a cross platform and both hardware and software independent
markup language
 It can be used to transfer structured data between heterogeneous systems.
 It also used as data interchange format in a number of applications. Wireless
markup language is used for web applications.

DIFFERENCES BETWEEN SGML, HTML, and XML


 A markup language uses tags to indicate how a file should look when
displayed on a webpage.
 SGML was released in 1980.it is complex and difficult for developers to
master.
 HTML is based on SGML.HTML provides a simple and an effective way to
create web pages.
 It is used to specify the appearance of the webpage.
 For document processing and data interchange, World Wide Web consortium
(W3C) developed XML.
 HTML is used for data presentation, whereas XML is used for data description
and definition.

NEED FOR XML


 During the late 1960s, industry groups, such as railroads, airlines, and
shipping companies, realized that processing a large of documents resulted in
significant delays in settlement and product delivery.
 As a result, electronic data interchange (EDI) emerged.

WHAT IS ELECTRONIC DATA INTERCHANGE


 EDI refers to the process of exchanging documents in a standard format
between two computers.
 Consider an example to understand the process of sending and receiving
documents by using EDI.
1. Company X creates a purchase order.
2. Company Y sends the purchase order through fax or e-mail.
3. Company Y receives the purchase order.
4. A representative from sales and invoicing division of company Y enters
the purchase order details into their computerized system.
5. Sales and invoicing division at company Y sends a copy of purchase
order to the warehouse through the computerized system or through
a paper document.
6. The warehouse packages the ordered goods.
7. Sales and invoicing division of company Y creates an invoice and sends
it to the shipping division.
8. The shipping division of company Y creates the necessary shipping
documents and dispatches the goods along with the shipping
documents and the invoice to company Y.
9. Upon receiving the goods, a representative from company X
acknowledges the receipt of the shipping goods and sends the receipt
back to company Y.
10. The shipping division of company X sends all the documents to the
accounting division.
11. The accounting division of company X makes a check sends it to
company Y.

HOW DOES EDI WORK


 A standard used for messages consists of consistent formats that are used for
electronic transmission of business documents.
 EDI entails a unidirectional transmission of information.
 EDI transmission involves the following steps:
1. The sender of information uses computer files to assemble the data
needed for transaction.
2. The assembled data is the input to a software module that transforms
the information into the EDI standard format.
3. The resulting data file generated by the software module is
transmitted to the receiver through an intermediate channel company
known as value added network (VAN).
4. The data file is treated as the input at the receiving end in which a
software module translates the data from the EDI format into a file
that can be understood by the receiver’s application system.
 Data security is maintained with the help of user identification number and
password.

LIMITATIONS OF EDI
EDI standard have various limitations. these includes:
 Rigid transaction set
 Fixed business rules
 High costs
 Slow pace of standards evolution

RIGID TRANSACTION SET


 The traditional EDI was built on fixed transaction set.
 This is a tedious and time consuming process because the standards
committee of XML must recognize the new format before it can be
implemented.

FIXED BUSINESS RULES


 The business rules of small, medium and large units of the same industry
vary widely.
 Due to this, the same set of EDI standards cannot be uniformly implemented
across businesses of disparate size.

HIGH COSTS
 Implementing EDI standard, can be expensive for small and medium sized
business units.
 When compared to large business units because VAN’ s are costly.

SLOW PACE OF STANDARD EVOLUTION

 EDI standards need to cater to many companies with different needs.


 EDI does not serve as a cost effective solution for implementing data
interchange across heterogeneous systems.

ADVANTAGES OF XML OVER EDI

 Organizations felt a need for a more effective way of communicating and


processing business data.
 Some of the advantages of XML over EDI are as follows:
1. lower cost
2. customized business rules
3. easy interpret
4. platform independent
Lower Cost
 XML does not require a costly VAN as required in EDI.
 The cost involved in XML based data interchange systems is lower as compare
to EDI.
Customized Business Rules
 It allows every individual business unit to have its own set of business rules
and formats.
 Business units can also change the format of various documents as per their
requirements.
 XML is more flexible as compared to EDI.
Easy to Interpret
 XML documents can be easily interpreted by the target systems by using a
parser.
 A parser is software program that checks the syntax used in XML files.
Platform Independent
 XML documents are platform independent; a business unit can interchange
data with any other business unit, regardless of the data storage platform.

ADVANTAGES OF XML
 some of the advantages offered by XML are as follows:
1. provides a way of creating domain specific vocabulary
2. allows data interchange between different computers
3. enables smart searchers
4. provides user selected view of data
5. allows granular updates

DOMAIN SPECIFIC VOCABULARY


 when you create web pages by using HTML, you are restricted to use of
specify tags.
 HTML uses a set of predefined tags for presenting data in various formats.
 Consider the following HTML code:
<B> my book</B>
<p>
John smith<br>
Tech books publications<br>
$30.00<br>
</p>
 The same information can be represented in a more meaningful way by using
XML:
<BOOK>
<AUTHOR>
John smith</AUTHOR><PUBLISHER>
Tech books publications
</PUBLISHER>
<PRICE>$30.00</PRICE>
</BOOK>
 The tags described the content instead of specifying the format in which the
content is to be displayed.

DATA INTERCHANGE
 Data interchange is essential to carry out business transactions.
 XML allows you to store data in structured text format.
 XML provides files that are unambiguous and easy to generate and read.

SMART SEARCHERS
 Although HTML provides you a set of predefined tags, implementing a search
on a HTML document is a difficult task.
The best picture award in 1999 went to the film<B>titanic.
</B>
This film was based on the story of a ship called <B>titanic.
</B>
 On the other hand , the flexibility to create user defined provided by XML:
The best picture award in 1999 went to the film
<FILM>titanic</FILM>
This film was based on the story of a ship called
<SHIP>titanic</SHIP>

USER SELECTED VIEW OF DATA


 In case of HTML, if you want to display the same information in different
formats, you need to create separate HTML documents.
 XML concentrates on data and not on the appearance in the data.
 To specify the appearance of data, you can use CSS and XSL.

GRANULAR UPDATES
 If you want to fetch the updated information from the server, the entire page
needs to be refreshed.
 The entire page need not be refreshed.
 Only the changed content needs to be downloaded.
ROLE OF W3C IN DEFINING XML-RELEATED SPECIFICATIONS
 XML was defined by W3C to ensure that structured data is uniform and
independent of vendors or application.
 Some of the rules are listed below:
1. XML must be directly usable over the internet.
2. XML must support a wide variety of applications.
3. XML must be compatible with SGML.
4. XML documents must be human-legible and clear.
5. XML design must be formal and concise.

WORKING WITH AN XML DOCUMENT


 Before you cerate an XML document, you need to understand how data is
stored in an XML document.
 It provides a way to store structured data that is capable of being recognized
by different kinds of devices.

COMPONENTS OF AN XML DOCUMENT


 An XML document is composed of a number of components that can be used
for representing information in a hierarchical order.
1. processing instruction
2. tags
3. elements
4. content
5. attributes
6. entities
7. comments
PROCESSING ATTRIBUTES
 XML document usually begins with a XML declaration statement, which is
also called the processing instruction (PI).
 The PI provides information regarding the way in which the XML document
should be processed.
 The PI statement must be written using lower case letters.
<?xml version = “1.0” encoding = “UTF-8”?>
 The PI statement uses the encoding property to specify the information about
the encoding scheme that is used to create the XML file.
 The encoding scheme is the standard character set of language.
 UTF (Unicode Transformation Format) -8 is the standard character that is
used to create pages written in English.
TAGS
 Tags are used to specify a name for a given piece of information.
 It is a means of identifying data. Data is marked up using tags.
<p>nick shaw</p>
 XML allows you to create your own tags, the same information can be stored
in the following way:
<EMP_NAME>nick shaw</EMP_NAME>

ELEMENTS
 Elements are basic units that are used to identify and describe data in XML.
 They are the building blocks of an XML document.
 Elements are represented using tags.
<Authorname>john smith</Authorname>
 XML allow you to provide meaningful names to elements.
 In preceding example, the Authorname element is used to store the names of
authors.
 An XML document must always have a root element. A root element
contains all other elements in the document.
<?xml version =”1.0”>
<AUTHORS>
<AUTHOR>
<FIRSTNAME> John</FIRSTNAME>
<LASTNAME>Smith</LASTNAME>
</AUTHOR>
</AUTHORS>
 The AUTHOR element contains all other elements in the XML document and
is, therefore, called the root element.
CONTENT
 The information that is represented by the elements of an XML document is
referred to as the content of that element.
<BOOKNAME>The painted house</BOOKNAME>
 XML enables you to declare and use elements that can contain different kinds
of information.
 An element can contain any of the following types of content:
1. character or data content
2. element content
3. combination or mixed content
Character or Data Content
 Elements that contain only textual information are said to have character or
data content.
<BOOKNAME>The painted house</BOOKNAME>
 In this example , the BOOKNAME element contains only textual information.
Element Content
 Elements can contain other elements.
 The elements contained within another element are called child elements.
 The containing element is called the parent element.
 A parent can contain many child elements.
<AUTHOR>
<FNAME> JOHN </FNAME>
<LNAME> SMITH> </LNAME>
</AUTHOR>
Combination or Mixed Content
 Elements can contain textual information as well as other elements.
<PRODUCT DESCRIBTION>
The product is available in two colors.
<COLOR> RED </COLOR>
<COLOR> BLUE </COLOR>
</PRODUCT DESCRIBTION>
ATTRIBUTES
 Elements can have one or more attributes. Attributes provide additional
information about elements.
 Attributes can be either mandatory or optional.
 An attribute consists of a name value pair.
<PRODUCTNAME PRODID = “P001”>Barbie doll</PRODUCTNAME>
 You may decide to present information either using elements or attributes.
There are no rules governing this decision.
1. If data must be displayed, you can represent it as an element.
2. If data must be updated frequently, it is better represented as an
element because it is easier to edit elements than attributes with xml
editing tool.
3. If the value of piece of information must be checked frequently, it may
be represented as an attribute.
ENTITIES
 An entity can be described as a shortcut to a set of information.
 It is a name that is associated with in a block of data.
 When an entity is used in XML file, it expands to its full definition.
<COMMENT> this article appeared in &nyt ; on Dec 12, 2000 </COMMENT>
 When you open the document containing the preceding code in a browser, it
displays the text “This article appeared in New York Times on Dec 12, 2000”.
 The entity nyt acts as a short name for “New York Times”.
 XML supports the use of three kinds of entities: internal, general and
parameter entities.
Internal Entities
 Some characters cannot be used in XML documents because they have a
special meaning.
 Ex: < is used as a delimiter for tags.
 XML provides some predefined entities to enable you to express such
characters in an XML document.
 These predefined entities are called internal entities.
 It consists of a name that is associated with a block of information.
 This can be easily identified as it is always preceded by an ampersand (&)
symbol and terminated with a semicolon.
&It; - used to display the less than (<) symbol.
&gt; - used to display the greater than (>) symbol.
&amp; - used to display the ampersand (&) symbol.
&quot; - used to display the double quote(“) character.

COMMENTS
 Comments are statements used to provide documentation information about
an application comments are essential.
 It is a good programming practice to include comments along with the code.
 XML comments are created by using an opening angular brackets by an
exclamation mark and two hyphens (<!__) and closing angular brackets
followed by two hyphens(__>).
<!__PRODUCTDATA is the root element-> // valid
</-PRODUCTDATA is the – root element__> //invalid

RULES FOR CREATING WELL-FORMED DOCUMENTS


 Following are the rules that govern the creation of a well-formed document:
Opening and Closing tags
 Every opening tag must have a closing tag. Every XML document must have a
single root element.
 All other elements used in the document must be embedded within the
opening and closing tads of the root element.
<OL>
<LI> this is the first bulleted item</LI>
<LI>this is the second bulleted item</LI>
</OL>
Empty Elements
 Empty elements must be closed using a forward slash (/).
 Empty elements do not contain any information.
 They can contain attributes.
 The values of these attributes are specified within opening and closing
angular brackets of the empty tags.
<PICTURE name=”mypicture.gif”/>
Attribute Values
 All attribute values must be presented in double quotation marks.
<FONT size = “12”>
Nesting Tags
 One tag within another tag.
 Tags must ne nest correctly.
 Opening tags must be closed in the reverse order in which they appear.
 The innermost tags must be closed before the outer tags.
<AUTHOR> John <BOOKNAME> The Client </AUTHOR></BOOKNAME>.
Case Sensitivity
 XML tags are case-sensitive.
 They must match each other in every implementation.
 The opening and closing tags must correspond in every respect.
 Any difference will result in an error.
<PRICE> 250 </price> //incorrect
<PRICE> 250 </PRICE> //correct
Creating an XML Document
 Cyber Shoppe requires a centralized repository of data about the products
sold through its e-commerce site.
 It has three branches, which maintain data on their local computer systems.
 Data from all the three available to all the departments of these branches.
 The product details consist of the name of the product, a brief description of
the product, the price and the available quantity.
 Each product is uniquely identified by a product ID.

<?xml version = “1.0” encoding = “UTF-8”?>


<!—PRODUCTDATA is the root element
<PRODUCTDATA>
<PRODUCT PRODID = “P001”>
<PRODUCTNAME>Barbie Doll</PRODUCTNAME>
<DESCRIPTION> This is a toy for children in the age group of 5-10 years
</DESCRIPTION>
<PRICE> $20.00</PRICE>
<QUANTITY>12</QUANTITY>
</PRODUCT>
</PRODUCTDATA>
 Save the file as product.xml by selecting Save from the File menu.
Viewing the XML Document in a Browser
 To view the XML document in the browser, open the product.xml file in
Internet Explorer 5.0, which provides a default layout for an XML document.
 This default layout displays the XML document in a tree view.
 When you open product.xml in Internet Explorer 5.0, it is displayed in the
tree view.
 You can expand and collapse the tree to see the contents of a particular
element.

STRUCTURE OF AN XML
 In order to make an application more flexible, you need to work with various
options, such as the appearance or the structure of a document.
 Both HTML and XML are used to publish documents on the Web through they
serve different purposes.
 HTML has a fixed set of elements.
 XML does not define any elements but allows you to define the structure per
your requirements.
 XML allows you to store data in a meaningful structure.
 If two trading partners have to interchange data the structure in which they
store the data should be consistent.
 To represent data in a consistent format, it needs to be given a meaningful
structure.
 To represent data in a uniform manner, you define the structure of an XML
document.
 The following XML related specifications to define the structure of an XML
document:
1. document type definition(DTD)
2. XML schema definition(XSD)

DOCUMENT TYPE DEFINITION (DTD)


 A DTD defines the structure of the content of an XML document. DTD allows
you to store data in a consistent format.
 A DTD specifies the elements that can be present in an XML document, the
attributes of these elements, and their arrangement with relation to each
other.
 It also allows you to specify whether an element or an attribute is optional or
mandatory.
 Creating a DTD is similar to creating a table in a database.
 In DTDs, you specify the structure of data by declaring elements to represent
the data.
 This is similar to creating columns in a table.
 You can store the data in an XML document that conforms to the DT for an
application.
 This is similar to adding records to a table.
 XML allows you to create your own DTD’s for applications.
 This gives you a complete control over the process of checking the content
and structure of XML documents created for an application.
 This checking process is called validation and confirm to a DTD are considered
valid documents.

DECLARING ELEMENTS IN A DTD


 To store structured data in an XML document, you need to declare elements
in a DTD. An XML document can then de checked against the DTD.
Syntax:
<!ELEMENT elementtype (content-type or content-model)>
 In the preceding syntax,
1. elementname specifies the name of an element.
2. content-type or content-model specifies whether the element
contains textual data or other elements.
 While declaring elements or attributes, you must consider some naming
conventions. They are:
1. A name consists of at least one letter. A letter may be in uppercase or
lowercase.
2. Name must start with an underscore.
3. One or more letters, digits, hyphens, underscores, or full stops can
follow the initial letter.
4. Spaces and tabs are allowed.
5. Only two punctuation signs, hyphen and period are allowed.
 An element can be of the following types:
1. empty: empty elements have no content and are marked up as<empty-
element/>
2. unrestricted: the opposite of the entry element is an unrestricted
element, which can contain any element declared elsewhere in a DTD.
3. container: container elements can contain character data and other
elements.
DECLARING EMPTY ELEMENT
 <!ELEMENT emptyelement EMPTY>
 An empty element is declared by specifying content type as EMPTY.
 It can contain attributes.
 It cannot contain textual content or other elements.
DECLARING UNRESTRICTED ELEMENTS
 An unrestricted element can be declared by specifying the content type as
ANY.
 <!ELEMENT anyelement ANY>
 Anyelement can contain any type of data, including other elements that are
declared elsewhere in a DTD.
DECLARING CONTAINER ELEMENTS
 Using element declaration in a DTD, you can specify which elements are
allowed inside an element, how often they may appear, and in what order.
 You do this by specifying the element content model.
<BOOK>
<TITLE> IF TOMORROW COMES </TITLE>
<AUTHOR> SIDNEY SHELDON </AUTHOR>
</BOOK>
 You need to create a DTD that contains declaration for three elements:
BOOK, TITLE and AUTHOR.
 You also need to decide whether TITLE and AUTHOR are mandatory or
optional, whether they can be in any order or have to be in a specific order,
and the number of times they can appear in an XML document.
 If both TITLE and AUTHOR have to specified and TITLE should be followed by
AUTHOR, the DTD would be written as:
<!ELEMENT BOOK (TITLE,’AUTHOR)><!-Element content -><!ELEMENT
TITLE (#PCDATA)><!- Character content -><!ELEMENT AUTHOR
<#PCDATA)><!-Character content->
 In a DTD, different symbols are used to specify whether an element is
mandatory or optional and whether it can occur more than once.
SYMBOLS USED IN DTD
SYMBOLS MEANING EXAMPLE DESCRIPTION
“and” TITLE, AUTHOR TITLE and AUTHOR
,
in that order
| “or” TITLE | AUTHOR TITLE or AUTHOR
“optional”, can AUTHOR? AUTHOR need not
occur only once be present, but if it
?
within the parent is present, it can
element occur only once.
Can be zero or (TITLE |AUTHOR)* Any number of
multiple TITLE or AUTHOR
*
occurrences of the elements can be
element order. present in any
At least one AUTHOR+ Can have multiple
occurrence of the AUTHOR elements.
element, can have
+ multiple
occurrences within
the parent
element.

DECLARING ATTRIBUTES

 In addition to declaring elements, you can also declare attributes in a DTD.


 These declarations are used during the validation process to check the
structure of an XML document.
Syntax:
<!ATTLIST elementname attributename valuetype [attributetype] [*default*]>
 You can assign values to attributes. To do so, you need to know the different
types of value that can be assigned to attributes.
Value Types used in DTD
VALUE TYPES DESCRIPTION
PCDATA used to represent plain text value
used to assign a unique value to each
ID element in the document; must begin
with an alphabetic character.
used to assign a specific range of
values; values are specified
(enumerated)
within parenthesis.

 In addition to specifying the value type of an attribute, you also need to


specify whether the attribute is optional or mandatory.
Attribute Types used in DTD
ATTRIBUTE TYPES DESCRIPTION
If the attribute of an element is
specified as #REQUIRED, then the
value for that attribute must be
specified each time the element is
REQUIRED
sued in an XML document. If the
value for the REQUIRED attribute is
not specified, the XML document will
be invalid.
If the attribute of an element is
specified as #FIXED, then the value of
FIXED
the attribute cannot be changed in an
XML document.
If the attribute of an element is
specified as #IMPLIED, then the
attribute is optional. An IMPLIED
IMPLIED
attribute can take text strings as its
values.

<!ATTLIST PRODUCT PRODID #REQUIRED>


 You can also specify the default value for an attribute.
<!ATTLIST PRODUCT CATEGORY (TOY/BOOK) “TOY”>

TYPES OF DTD’ s
 A DTD can be part of an XML document or can be a separate file containing
declarations of elements and attributes.
 DTD can be classified into two types, internal and external DTD’ s.
Difference between Internal and External DTDs
INTENAL DTD EXTERNAL DTD
A part of an XML document. It is maintained as a separate file. A
reference to this file is included in an
XML document.
Can be used only by the document in It can be used across multiple
which it is created and cannot be documents.
used across multiple documents.

 To ensure that the structure of an XML document conforms to a DTD, you


must associate the DTD with the XML document.
 The <!DOCTYPE> declaration is used to associate a DTD with an XML
document.
SYNTAX
 The syntax for defining a internal DTD in an XML document is as follows:
<!DOCTYPE rootelement
[element and attribute declarations]>
 The syntax for referencing an external DTD in an XML document is as follows:
<!DOCTYPE rootelement PUBLIC/SYSTEM “path-of-file”>
In the preceding declaration,
 Rootelement represents the name of the root element.
 PUBLIC specifies that the DTD is stored on a server whereas the SYSTEM notation
specifies that the DTD is stored on the local system.
 Path-of-file represents the name of the DTD file along with the path of the file.
VALIDATING THE STRUCTURE OF DATA
 To validate the structure of the data stored in an XML document against a DTD,
you need to use parsers.
 Parsers are software programs that check the syntax used in an XML file.
 There are two types of parsers. They are:
1. Nonvalidating parsers.
2. Validating parsers.
Nonvalidating Parsers
 A nonvalidating parser checks if a document follows the XML syntax rules.
 It builds a tree structure from the tags used in XML document and returns an
error only when there is a problem with the syntax of the document.
 Nonvalidating parsers process a document faster than a validating parser
because they do not have to check every element against a DTD.
 These parsers check whether an XML document adheres to the rules of well-
formed documents.
 The Expat parser is an example of a nonvalidating parser.
Validating Parsers
 A validating parser checks the syntax of the elements, builds the tree structure of
an XML document, and compares the structure of an XML document with the
structure specified in the DTD associated with the document.
 In addition to checking whether an XML document is well formed, validating
parsers also check whether the XML document adheres to the rules in the DTD
used by the XML document.
 The Microsoft MSXML parser is an example of a validating parser.

CREATING AN XML DOCUMENT BY STORING DATA


 Consider a scenario to understand how an XML document can be created and
validated against a DTD.
 The head office of CyberShoppe sends information about its products to its
various branch offices.
 The product details must be stored in a consistent format at all the branches.
 Certain restrictions must be placed on the kind of data that can be saved in the
data store to ensure the uniformity and consistency of information.
 The products sold are organized into two categories, toys and books.
List of Elements
ELEMENTS DESCRIPTION
Indicates that data about products is being
PRODUCTDATA stored in XML document; act as the root
element for all other elements
Represents the details (product name,
PRODUCT description, price, and quantity) for each
product
PRODUCTNAME Represents the name of the each product
Represents the description of each
DESCRIPTION
product
PRICE Represents the price of each product
QUANTITY Represents the quantity of each product
Attributes Used by DTD
ATTRIBUTES DESCRIPTION
Represents a unique identification value
PRODUCTID
for each product
Represents the category of a product and
CATEGORY specifies whether a product is a toy or a
book
Content Type Used for Each Element
ELEMENT CONTENT TYPE DESCRIPTION
Contains one or more
PRODUCTDATA Element content
PRODUCT elements
Contains the details of
other products and
therefore, will contain the
PRODUCT Element content
elements PRODUCTNAME,
DESCRIPTION, PRICE, and
QUANTITY
Contains regular text that
PRODUCTNAME Data content represents the name of the
product
Contains regular text that
DESCRIPTION Data content represents the description
of the product
Contains regular text that
PRICE Data content represents the price of the
product
Contains regular text that
QUANTITY Data content represents the quantity of
the product
Attributes Used in DTD
ATTRIBUTES ATTRIBUTE TYPES VALUE TYPES DESCRIPTION
Product ID must
have a unique value
PRODUCTID #REQUIRED ID and has to be
specified for every
product.
The category must
CATEGORY #REQUIRED (enumeration) be either TOYS or
BOOKS.
 To declare elements and attributes in a DTD for the given scenario, use the
following code.
< ! ELEMENT PRODUCTDATA (PRODUCT) + >
< ! ELEMENT PRODUCT (PRODUCTNAME, DESCRIPTION, PRICE, QUANTITY) >
< ! ELEMENT PRODUCTNAME (#PCDATA) >
< ! ELEMENT DESCRIPTION (#PCDATA) >
< ! ELEMENT PRICE (#PCDATA) >
< ! ELEMENT QUANTITY (#PCDATA) >
< ! ATTLIST PRODUCT PRODUCTID ID # REQUIRED
CATEGORY (BOOKS | TOYS) “TOYS”>
 Save the file as products.dtd
 To store the data about products in an XML file, type the following code and save
the file as products.xml in the same folder where the DTD file is saved.
< ? xml version = “1.0” >
< ! DOCTYPE PRODUCTDATA SYSTEM “products.dtd”>
<PRODUCT PRODUCTID = “P001” CATEGORY = “BOOKS” >
<PRODUCTNAME> Gone With the Wind </PRODUCTNAME>
<DESCRIPTION>The backdrop of this book is the American Civil
War</DESCRIPTION>
<PRICE> 25.00 </ORICE>
<QUANTITY> 35 </QUANTITY>
</PRODUCT>
</PRODUCTDATA>

 The use of the < ! DOCTTTYPE> declaration in the XML document, used to
associate the external DTD products.dtd with the XML document.
XML Schema
 An XML Schema is used to define the structure of an XML document.
 Microsoft has developed a language that is used to define the schema of an XML
document.
 This language is called the XML Schema Definition (XSD) language.
 XML schemas have now become a W3C recommendation for creating valid XML
documents.
Advantages of XML Schemas over DTDs
 An XML schema created using XSD is similar to a DTD.
 An XML schema created by using XSD has many advantages over DTD.
 Some of the advantages are:
1. XSD provides more control over the type of data that can be assigned to
elements and attributes as compared to DTD.
2. DTD does not enable you to define your own customized data types. XSD
enable you to create your own data types.
3. XSD also allows you to specify restrictions on data.
4. The syntax for defining a DTD is different from the syntax used for
creating an XML document. The syntax for defining an XSD is the same as
the syntax of an XML document.
Data Types Used in an XML Schema
 A data type specifies the type of content that an element can hold.
 XSD provides a list of predefined data types.
 These data types can be classified as:
1. Primitive
2. Derived
3. Atomic
4. List
5. Union
Primitive Data Types
 Primitive data types are the fundamental data types.
 These data types are the basis for defining other data types.
 Primitive data types do not contain elements or attributes.
 They contain only values.
Primitive Description
String Represents a collection of characters
Represents decimal numbers. This can
Decimal
be used to define a value accurately
Represents single-precision 32-bit
Float
floating-point numbers
Represents Boolean values. This data
Boolean
type can have true or false as its value.
TimeDuration Represents a duration of time
Represents an activity that recurs after
RecurringDuration
a specific time duration
Derived Data Types
 Derived data types are defined using other data types called base types.
 Base data types be either primitive or derived data types.
 Derived data types can be built-in or user-defined.
Derived Data Type Base Data Type Description
Integer Decimal Represents a sequence of
decimal digits with an
optional leading sign
Long Integer Represents a value within
the range of
9223372036854775808and
9223372036854775807
nonNegativeInteger Integer Represents an integer that
is greater than or equal to
zero
PositiveInteger NonNegativeInteger Represents an integer
greater than zero
Int Long Represents an integer that
can have a minimum value
of 2147483648 and a
minimum value of
2147483647
Time RecurringDuration Represents an instance of
time that recurs everyday
Date TimeDuration Represents a time period
that starts at the midnight
of a particular day and ends
the next midnight
Atomic Data Types
 Atomic data types are those data types that cannot be broken down into smaller
units.
 These data types can be primitive or derived.
 String is a primitive data type that cannot be broken down further into small
units.
List Data Types
 List data types are derived data types that contain a set of values of an atomic
data type.
 For example, you can create a list data type called ‘pointlist’ derived from the
data type decimal, which can contain the values 0.5, 0.25, and 0.75.
 Any element that is associated with the data type, pontlist can contain only 0.5,
0.25, and 0.75 as its value.
Union Data Types
 Union data types are derived from atomic and list data types.
 For example, ‘zipnum’ is a list data type that contain a combination of both.
<ZIPCODE> NY </ZIPCODE>
<ZIPCODE> 90547 96098 90878 </ZIPCODE>
<ZIPCODE> CA <ZIPCODE>
 In addition to the predefined data types, XSD also allows you to define custom
data types.
 The custom data types can be classified as:
1. Simple Data Type: A data type that contains only values.
2. Complex Data Types: A data type that contains child elements, attributes,
and mixed content.
Declaring a Simple Type Element
 A simple type element does not contain any child elements or attributes.
 It contains only values such as numbers, strings and dates.
Syntax:
<xsd: element name = “element-name” type = “data type” min occurs =
“nonNegativeInteger” maxoccurs = “nonNegativeInteger|unbounded”/>
Attributes of an Element
Attributes Description
Name Specifies the name of the element being
declared.
Type Specifies the data type of the element
being declared.
MinOccurs Specifies the minimum number of times
an element can occur. If you specify
minOccurs as zero, it means that the
element is optional. If you set it to a
value greater than zero, it means that
the element is mandatory and must
occur at least the specified number of
times.
MaxOccurs Specifies the maximum number of times
an element can appear, if you specify
maxOccurs as unbounded, it means that
the element can appear any number of
times in an XML document.

<PRODUCTDATA>
<PRODUCT PRODUCTID = “P001” CATEGORY = “BOOKS”>
<PRODUCTNAME> Gone with the Wind </PRODUCTNAME>
<DESCRIPTION> The backdrop of this book is the American Civil War
</DESCRIPTION>
<PRICE> 25.00 </PRICE>
<QUANTITY> 35 </QUANTITY>
</PRODUCT>
</PRODUCTDATA>
 You can create Simple data types to the above code as follows:
<xsd:element name = “PRODUCTNAME” type = “xsd:string”/>
<xsd:element name = “DESCRIPTION” type = “xsd:string”/>
<xsd:element name = “PRICE” type = “xsd:positiveInteger”/>
<xsd:element name = “QUANTITY” type = “xsd:nonNegativeInteger”/>
 To associate an element with a user-defined simple type, you must define the
new simple type.
<EMPNAME> JOHN SMITH </EMPNAME>
<EMPADDRESS> 10, LIONS STREET, BOSTON </EMPNAME>
<EMPPHONE> 172-693-2248 </EMPPHONE>
 You can specify various facet values on elements by creating a new simple type
based on an existing data type.
 Facets are the various attributes of a simple type that determine the values for a
simple type.
 Consider the following code:
<xsd : simpletype name = “phoneno”>
<xsd : restriction base = “xsd : string”>
<xsd : length value = “10”/>
<xsd : pattern value = “\d{3} - \ d{3} - \ d{4}”/>
</xsd : restriction> </xsd : simpletype>
Facets of a String Data Type
Data Type Description
Length Specifies the number of characters the
data type can contain, value must be a
nonNegativeInteger.
MinLength Specifies the minimum length of the
values that the data type must contain,
value must be nonNegativeInteger.
MaxLength Specifies the maximum length of the
values that the data type must contain,
value must be nonNegativeInteger.
Pattern Specifies the regular expression that the
data type’s values must match.
Enumeration Specifies the set of values that the data
type can contain
Creating a Complex Type Element
 A complex type element is one of that contains other elements, attributes, and
mixed content.
 To declare a complex type element, you need to first define a complex type.
 You can then declare a complex element by associating the complex type with
the element.
 You can define a complex type by using the syntax :
<xsd : complex Type name = “data type name”>
Content model declaration
</xsd : complexType>
 in the cybershopper scenario, the element PRODUCT is a complex element.
 To declare the element PRODUCT, you need to first create a complex data type,
as belown:
<xsd : ComplexType name = “prdt”>
<xsd : sequence>
<xsd : element name = “PRODUCTNAME” type = “xsd : string”/>
<xsd : element name = “DESCRIPTION” type = “xsd : string”/>
<xsd : element name = “PRICE” type = “xsd : positiveInteger”/>
<xsd : element name = “QUANTITY” type = “xsd : nonNegativeInteger”/>
</xsd : sequence>
</xsd : complexType>
 The data type is referenced by a name, it is called a named data type.
 A named data type is one in which a set of elements and attributes are
referenced by a single name.
 At the time of declaring an element, this data type is referenced by the type
identifier.
 Named data types can be identified by the type identifier in the element
declaration statement.
 Named data types can be then be reused in any part of a schema.
 Data types that are not referenced by a common name are called anonymous
data type.
 These data types can be identified by the absence of the type identifier in the
element declaration statement.
 Anonymous data types can be referenced only once in a schema.
<xsd : element name = “PRODUCT”/>
<xsd : complexType> <xsd : sequence>
<xsd : element name = “PRODUCTNAME” type = “xsd : string”/>
<xsd : element name = “DESCRIPTION” type = “xsd : string”/>
<xsd : element name = “PRICE” type = “xsd : positiveInteger”/>
<xsd : element name = “QUANTITY” type = “xsd : nonNegativeInteger”/>
</xsd : sequence>
</xsd : complexType>
Creating an XML Document
 An XML file cannot be directly associated with an XML schema file.
 An XML file can be associated with an XML schema only through a validator.
 Consider a scenario to understand how an XML document can be created and
validated against a DTD.
 The head office of CyberShoppe sends information about its products to its
various branch offices.
 The product details must be stored in a consistent format at all the branches.
 Certain restrictions must be placed on the kind of data that can be saved in the
data store to ensure the uniformity and consistency of information.
Elements Used in XSD
ELEMENTS DESCRIPTION
Indicates that data about products is being
PRODUCTDATA stored in XML document; act as the root
element for all other elements
Represents the details (product name,
PRODUCT description, price, and quantity) for each
product
PRODUCTNAME Represents the name of the each product
Represents the description of each
DESCRIPTION
product
PRICE Represents the price of each product
QUANTITY Represents the quantity of each product

Data Types of the Element


ELEMENT DATA TYPE DESCRIPTION
A complex type element
that can hold other
PRODUCTDATA Complex data type
elements, attributes, and
mixed content
A complex type element
that can hold other
PRODUCT Complex data type
elements, attributes, and
mixed content
A simple type element that
PRODUCTNAME String contains values of the
string data type.
A simple type element that
DESCRIPTION String contains values of the
string data type.
A simple type element that
contains values of the
PRICE positiveInteger positiveInteger data type.
(product price must be
greater than zero)
A simple type element that
QUANTITY Integer contains values of the
integer data type.
 You need to define simple and complex elements for the XML document.
 The simple elements can be declared in the XSD as follows:
xsd : element name = “PRODUCTNAME” type = “xsd : string”/>
<xsd : element name = “DESCRIPTION” type = “xsd : string”/>
<xsd : element name = “PRICE” type = “xsd : positiveInteger”/>
<xsd : element name = “QUANTITY” type = “xsd : nonNegativeInteger”/>
 Next, you create an XML document that needs to be validated against an XML
schema.
 To create the XML document, type the following code and save as products.xml:
< ? xml version = “1.0” >
<PRODUCTDATA>
<PRODUCT>
<PRODUCTNAME> Barbie Doll </PRODUCTNAME>
<DESCRIPTION>This is a doll for children aged 11 and above
</DESCRIPTION>
<PRICE> 200 </ORICE>
<QUANTITY> 12 </QUANTITY>
</PRODUCT>
</PRODUCTDATA>
Creating an XML Schema
 You can integrate the various components of a schema by using the schema
element.
 To create an XML schema for declaring the elements and attributes required to
store data, type the following code.
 And save the file as products.xsd
<xsd : schema xmlns : xsd = “http://www.w3.org/2001/XMLSchema>
<xsd : element name = “PRODUCTDATA” type = “prdata”/>
<xsd : complexType name = “prdata”>
<xsd : sequence>
<xsd : element name = “PRODUCT” type = “prdt”/>
</xsd : sequence>
</xsd: complexType>
<xsd : ComplexType name = “prdt”>
<xsd : sequence>
<xsd : element name = “PRODUCTNAME” type = “xsd : string”/>
<xsd : element name = “DESCRIPTION” type = “xsd : string”/>
<xsd : element name = “PRICE” type = “xsd : positiveInteger”/>
<xsd : element name = “QUANTITY” type = “xsd : nonNegativeInteger”/>
</xsd : sequence>
</xsd : complexType>
</xsd : schema>

UNIT - V
Declaring Attributes
 While creating an XML schema, you define a set of elements in the schema to
provide a structure to the XML document.
 Defining elements only might not help you to structure the XML document in
a desired format to achieve result, you might want to further qualify elements.
 To further qualify elements, you can use attributes. You can declare attributes
in an XML schema.
 To declare an attribute, you can use the attribute element.
The attribute Element
 You can declare an attribute in an XML schema by using the following syntax:
<attribute name = “attributename” ref = “attributename” type =
”datatypename” use = ”value” value =”value”> </attribute>
 The attribute element contains attributes that are used to further qualify and
restrict the scope and use of a user-defined attribute.
 The attributes of the attribute element are:
1. Name
2. Ref
3. Type
4. Use
The name Attribute
 The name attribute is used to specify the name of a user-defined attribute.
 This attribute must be used when the schema element is the parent element of
the attribute element.
 The value of the name attribute cannot include the colon (:) symbol.
The ref Attribute
 The ref attribute is used to refer to a user-defined attribute declared in the same
or any other XML schema.
 You cannot use the name and ref attribute together in the attribute element.
The type Attribute
 The type attribute takes a value that specifies the data type of a user-defined
attribute.
Example:
<xsd:attribute name = “ PRODUCTID” type = “xsd:string”>
 The PRODUCTID attribute is declared.
 The type attribute is set to string, which is a simple data type.
 This ensures that a user can assign an alphanumeric value to the attribute.
 Attributes can be associated with both built-in and user-defined simple types.
 For example, to associate a user-defined simple type called myID with an
attribute called PRODID, you can use the following declaration statement:
<xsd:attribute name = “PRODID” type = “myID”>
 In the preceding example,myID is a user-defined simple type, which is created
using the simple Type element.
The use Attribute
 The use attribute specifies the way in which an attribute can be used in an
XML document.

Table: values defined with the Use Attribute


Value Description Example
optional Specifies that the use of the user- <xsd:attribute name =
defined attribute in the XML document “baseprice” type =
is not compulsory. This is the default “xsd:integer” use =
values for the use attribute. This means, “optional”
if the use attribute is not specified in an />
attribute declaration, then the value
contained by this attribute is taken to
be optional by default.
Default Specifies a default value for the user- <xsd:attribute name =
defined attribute. If the value of the “baseprice” type =
attribute is not specified in the XML “xsd:integer” use =
document, the default value is used “default” value=”25”/>
instead. A default value is a way of
ensure that an attribute is always set to
some value. This default value is
specified using the value attribute of
the attribute element
required Specifies that the attribute must appear <xsd:attribute name =
in the XML document that conforms to “baseprice” type =
the schema. If you do not specify the “xsd:integer” use =
value for a required attribute, an error “required”
is generated when you try to validate />
the XML document against the schema.
Fixed Specifies that the user-defined attribute <xsd:attribute name =
has a fixed value that cannot be “baseprice” type =
changed in the XML document. You “xsd:positiveinteger” use =
must specify the value for the fixed “fixed” value=”600”>
attribute by using value attribute

 The following example illustrates the use of the attribute element in an XML
schema:
<xsd:element name = “PRODUCT” type = “prod” />
<xsd:complexType name = “prod”>
<xsd:sequence>
<xsd:element name = “PRODUCTNAME” type = “xsd : string” />
<xsd:element name = “DESCRIPTION” type = “xsd : string” />
<xsd:element name = “PRICE” type = “xsd : positiveInteger” />
<xsd:element name = “QUANTILY” type = “xsd : Integer” />
</xsd : sequence>
<xsd:attribute name = “PRODID” type = “xsd : string” use =
“required”>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
 An element called PRODUCT is declared. This element is associated with a
complex type called prod.
 This complex type contains the declarations for the elements
PRODUCTNAME, DESCRIPTION, PRICE and QUANTITY
Global Attributes
 You might need to use the same attribute for different elements.
 For example, You have two elements, book and author in an XML document
and you want to use an attribute called name with both the elements
 Global attribute are attributes that are declared outside all element
declaration.
 Global attributes facilitate reusability of attributes. For such attribute, the
schema element is the parent element.
Example:
<xsd : schema>
<xsd : attribute name = “NAME” type = “xsd:string” />
</xsd : schema>
 The NAME attribute is declared directly under the schema element.
 Therefore, it is global attribute.
 After declaring a global attribute, you can reuse this attribute from anywhere
within the schema.
Restricting values
 Attribute are declared in an XML schema, values are assigned to attribute in
an XML document that conforms to the XML schema
 You might need to ensure that the values assigned to attributes match a certain
pattern or contain one of the specified sets of values.
 This information can be stated in the XML schema
 To restrict the values that can be assigned to a user-defined attribute:
1. Declare the attribute and associate them with user-defined simple types.
2. Create the simple types by using the simpleType element.
3. Use the restrict element within the simpleType element.
Example:
<xsd:attribute name = “PRODID” type = “pID” use = “required”/>
<xsd:simpleType name = “pID”>
<xsd:restriction base = “xsd:string”>
<xsd:pattern value = “[p] {1} \d{3} “/>
</xsd:restriction>
</xsd:simpleType>
Using Attributes in an XML Schema
 You have learned about the attribute element and the use of that element in an
XML schema.
 Cybershopper sells two categories of products, books and toys.
 The product details comprise the name of the product
 To create an XML schema based on the details given in the scenario
 You need to identify the elements and attributes of the XML schema
Element of the XML Document
Element Description
PRODUCTDATA Acts as the root element for all other element that are used
in the XML document
PRODUCT Represent the details (product name description, price and
quantity)for each product
PRODUCTNAME Represents the name of each product
DESCRIPTION Represents the description of each product
PRICE Represents the price of each product
QUANTITY Represents the available quantity of each product

 To declare the elements and attributes required for storing products data type
the following code in Notepad and save it as product.xsd.

Attributes of the XML Document


Attributes Description
Represents the ID of each product and
PRODID
uniquely identifies that product.
Represents the category to which a
CATEGORY product belongs. The value of this
attribute can be set to either Toy or Book.
 To declare elements and attributes required for storing products data, needed
the following code and save this file as product.xsd.

<xsd : schema xmlns : xsd = “http://www.w3.org/2001/XMLSchema>


<xsd : element name = “PRODUCTDATA” type = “prdata”/>
<xsd : complexType name = “prdata”>
<xsd : sequence>
<xsd : element name = “PRODUCT” type = “prdt”/>
</xsd : sequence>
</xsd: complexType>
<xsd : ComplexType name = “prdt”>
<xsd : sequence>
<xsd : element name = “PRODUCTNAME” type = “xsd : string”/>
<xsd : element name = “DESCRIPTION” type = “xsd : string”/>
<xsd : element name = “PRICE” type = “xsd : positiveInteger”/>
<xsd : element name = “QUANTITY” type = “xsd : nonNegativeInteger”/>
</xsd : sequence>
<xsd : attribute name = “PRODID” type = “xsd : string” use = “required”/>
<xsd : attribute name = “CATEGORY” type = “cat” use = “required”/>
</xsd : complexType>
<xsd : simpleType name = “cat”>
<xsd : restriction Base = “xsd: String”>
<xsd : pattern value = “Toy | Book”/>
</xsd : restriction>
</xsd : simpleType>
</xsd : schema>
 After creating product.xsd, type the following code and save it as product.xml.

<?xml version = “1.0”>


<PRODUCTDATA>
<PRODUCT>
<PRODUCTNAME> Barbie Doll </PRODUCTNAME>
<DESCRIPTION>This is a doll for children aged 11 and above
</DESCRIPTION>
<PRICE> 75 </ORICE>
<QUANTITY> 54 </QUANTITY>
</PRODUCT>
</PRODUCTDATA>
XML Namespaces
 XML is an extensible language. This means that you can create and use your
own tags in an XML document.
 The rules for these tags are specified in the schema.
 The elements and attribute declared in a schema can also be called the
vocabulary for an XML document.
Example:
 Suppose you have declared the element BOOK in an XML schema.
 This element acts as the parent element for two child element, NAME and
PRICE.
<?xml version = “1.0”?>
<BOOK>
<NAME>Sense and Sensibility</NAME>
<PRICE>25</PRICE>
</BOOK>
Namespace
 A namespace is a method of preventing conflicts between elements having the
same names.
 In XML, a namespace is a virtual space that is assigned or recognized by
Uniform Resource Identifier(URI).
 It is a string that uniquely identifies the elements and attribute from different
schema.
 You can assign any name or string as URI. However, you can refer to the
following guidelines to ensure the uniqueness of the URI:
1. Using URI that is controlled by the developer: To ensure the
uniqueness of the URI, the recommended strategy is to use a domain
name that is under the developer’s control.
2. Using a relative URI: although there is no restriction on the use of
relative URIs, it is recommended that a fully qualified name be used to
specify URI.
 You can avoid the conflict between two schema by associating URI with the
elements or attribute that are used in an XML document.
Example:
<www.ecomatcybershoppe.com/authors:NAME>
 It may become tedious for a developer to use the complete URI with every
occurrence of the NAME element XML allows you to map a prefix to URI.
 You can think of a prefix as an alias for namespaces URI.
 Consider the following statement.
<xmlau = www.ecomatcybershop.com/author>.
 Above the xmlau is mapped to the namespace URI
www.ecomatcybershop.com/author.
 Now we can use this prefix instead of the complete namespace URI.
Declaring Namespaces
 A namespace can be declared in an XSD document by using the xmIns
keyword.
 The keyword is an attribute of the schema element that is declared at the
beginning of the document.
 The general form of the xmIns keyword is as follow:
xmIns : prefix = “URI”
 This prefix is optional
 There are two kinds of namespace declarations, default and explicit.
Default Declaration
 The default declaration statements enable you to declare a default namespace
for a document.
 In case of a default namespace you need not specify the prefix
 All element and attribute belonging default namespace can be used without
any prefix.
Example:
<schema xmIns = http://www.w3.org/2001/XMLSchema>
:
:
</Schema>
Explicit Declaration
 In an explicit declaration the xmIns keyword associates a prefix with a
namespace URI as shown in the following
Example:
<xsd:schema xmIns:xsd = http://www.w3.org/2001/XMLSchema>
:
:
</xsd:schema>
 In the preceding code, the xmIns keyword is used to associated the prefix xsd
with the namespace URL.
<xsd:element name = “PRODUCT” type = “xsd:string” use = “required”/>
 In the preceding code, the PRODUCT element is declared.
Reusing Schema Components
 One of the key features of schema is their ability to support a high degree of
reusability.
 The components declared in one schema can be reused by another schema.
 To reuse the components declared in one schema in another schema, you use
include and import elements.
The include Element
 The include element is used to include or refer to an external schema that is
located at a definite address.
Syntax:
<include id = “ID” schemaLocation = “filename” />
 The include element consists of two attribute id and schema Location.
 The include element can have multiple occurrences in an XML schema.
 The schema element is the parent element of the include element.
 The only restriction for the use of the include element is that both the
contained and contained schema files must belong to the same target
namespace.
 A target namespace has a reference to a URI to which a schema belong.
 You can create different target namespaces for different target namespaces for
different schemas.
 The include element allows reference to any external schema which is defined
in the context of the same target namespaces.
The import Element
 The import element performs the same function as the include element.
 The import element allows you to access components from multiple schema
that may belong to different target namespace.
Syntax:
<import id = “ID” namespace = “namespace” schemaLocation =
“filename” />
 The import element contains three attributes:
1. The id attribute takes the ID of the element as its value. This ID must
be unique for the XML schema. This attribute is optional.
2. The namespace attribute is used to specify the namespace URI to which
the imported schema belongs.
3. The schemaLocation attribute is identical to the one used by the include
element.
Reusable Data Types
 To reuse the components of an XML schema, you can store attributes in that
schema by using reusable data type.
 You can specify the restrictions on these data types.
 You can use the include or import element to refer these data types in another
schema that contains the declaration for elements and attributes of the
imported schema.
 To create reusable data type, you need to create an XML schema that contains
declarations for the data type.
 This XML schema can then be included in other schemas.
 Therefore, you can declare all commonly used data types in one schema and
reference the schema from other schemas.
Using the Components of One Schema in another Schema
 The branches of cybershopper purchase various products from their suppliers.
 The details of these purchase orders are sent to the head office for reporting
purpose.
 The branches send data in an XML format.
 The product ID and the order ID are used repeatedly in a number of
documents.
 The product ID begins with the letter P followed by 3 digits.
 The order ID begins with the letter O followed by 3 digits.
 These restrictions must be specified at a centralized location in such a way that
they can be applied across multiple documents.
 To accomplish the desired result, you need to perform the following tasks:
1. Identify the elements and attribute required to store data
2. Identify a mechanism for applying restrictions across multiple documents
3. Declare reusable data types
4. Declare the elements and attributes to be used for storing data
5. Create an XML document
Elements Required to Store Data about Purchase Orders
Element Description
PURCHASEORDER Acts as the root element for all other elements that are
used in the XML document
ORDER Represents the details (date, product name, supplier name,
address, quantity, and price per unit) for each order
DATE Represents the order date
PRODUCTNAME Represents the product name
SUPPLIERNAME Represents the supplier name
ADDRESS Represents the address of the supplier
QUANTITY Represents the quantity ordered for a product
PRICEPERQTY Represents the price per unit of a product

Attributes Required to Store Data about Purchase Orders


Attribute Description
ORDERID Represents the ID of an order
PRODID Represents the product ID
 To create reusable data types for storing product ID and order ID.
 Type the code and save the file as potype.xsd.
<schema xmlns = “http://www.w3.org/2001/XMLSchema “
targetNamespace = “www.ecomatcybershop.com/purchase”>
<simpleType name = “prstring”> <restriction base = “string”>
<pattern value = “[p] {1} \ d{3}”/>
</restriction>
</simpleType>
<simpleType name = “orstring”>
<restriction base = “string”>
<pattern value = “[0]{1}\d{3}”/>
</restriction>
</simpleType>
</schema>
 To declare the elements and attributes to be used for storing the purchase order
details, you need to create another XML schema.
 This schema should contain a reference to the schema specified in potype.xsd
to use the data type declared in potype.xsd.
 To do so, type the code and save the file as purchase.xsd.
<schema xmlns = “http://www.w3.org/2001/XMLSchema “
targetNamespace = “www.ecomatcybershop.com/purchase” xmlns :
prd = “www.ecomatcybershop.com/purchase”>
<include schemaLocation = “potype.xsd”/>
<element name = “PURCHASEORDER” type = “prd:purord”/>
<complexType name = “purord”> <sequence>
<element name = “ORDER” type = “prd:ord”/>
</sequence>
<attribute name = “PRODID” type = “prd:prstring”/>
<attribute name = “ORDERID” type = “prd:orstring” use =
“required”/>
</complexType>
<complexType name = “ord”>
<sequence>
<element name = “DATE” type = “date”/>
<element name = “PRODUCTNAME” type = “string”/>
<element name = “SUPPLIERNAME” type = “string”/>
<element name = “ADDRESS” type = “string”/>
<element name = “QUANTITY” type = “positiveInteger”/>
<element name = “PRICEPERQTY” type = “positiveInteger”/>
</sequence>
</complexType>
</schema>
 Now, you need to store the purchase order data in XML document.
 To do so, type the code and save the file as purchase.xml.
< ? xml version = “1.0” ?>
<prod : PURCHASEORDER xmlns:prod =
“www.ecomatcybershop.com/purchase” PRODID = “P001”
ORDERID = “0001”>
<ORDER>
<DATE> 200 – 02 – 13 </DATE>
<PRODUCTNAME>Barbie Doll</PRODUCTNAME>
<SUPPLIERNAME>Steve Shaw</SUPPLIERNAME>
<ADDRESS>15, LIONS STREET, BOSTON</ADDRESS>
<QUANTITY>3</QUANTITY>
<PRICEPERQTY>200</PRICEPERQTY>
</ORDER>
</prod : PURCHASEORDER>
Grouping Elements and Attributes
 XML schemas allow you to group related elements and attributes.
 It performs the following tasks:
1. Create a reusable group of elements and attributes:
 A reusable group can be used to specify the content model for a
complex type.
 This eliminates the task of declaring the elements, which have
already been declared in some other context.
2. Select a single element from a group:
 At times, you may want to use just one element or attribute
from a group.
 For example, you may want to specify either the residence or
the office phone number of an employee.
3. Specify the sequence of elements:
 You can create a group of elements and specify the sequence.
Grouping Elements
XSD provides a number of elements that can be used to group user-defined elements
and attributes.
1. Sequence
2. Group
3. Choice
4. All
5. attributeGroup
The sequence Element
 The sequence element helps you ensure that the elements declared within the
start and end tags of the element appear in a specific order.
Example:
<xsd:schema xmIns:xsd = http://www.w3.org/2001/XMLSchema>
<xsd:element name = “EMPLOYEE” type = “emptype” />
<xsd:complexType name = “emptype”> <xsd:sequence>
<xsd:element name = “FIRSTNAME” type = “xsd:string”/>
<xsd:element name = “LASTNAME” type = “xsd:string”/>
<xsd:element name = “DESIG” type = “xsd:string”/>
<xsd:element name = “DEPARTMENT” type = “xsd:string”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The group Element
 In XSD, a set of elements can be grouped by a common name and
incorporated into a complex type.
Syntax:
<group maxOccurs = “nonNegativeInteger” | “unbounded” minOccurs =
“nonNegativeInteger” name = “NCName” ref = “QName”> </group>
Attributes of the Group Element
Attribute Description

MaxOccurs Used to specify the maximum number of times a group


can occur in the XML document.
MinOccurs Used to specify the minimum number of times a group
can occur in the XML document
Name Used to assign a name for the groupelement. The name
assigned to the group must not contain any colon.
Ref Used to refer to a group in a complex type element.
Example:
<xsd:schema xmIns:xsd = http://www.w3.org/2001/XMLSchema>
<xsd : group name = “empname”>
<xsd : sequence>
<xsd:element name = “FIRSTNAME” type = “xsd:string”/>
<xsd:element name = “LASTNAME” type = “xsd:string”/>
</xsd: sequence>
</xsd: group>
<xsd:element name = “EMPLOYEE” type = “emptype” />
<xsd:complexType name = “emptype”> <xsd:sequence>
<xsd: sequence>
<xsd: group ref = “empname”/>
<xsd:element name = “ADDRESS” type = “xsd:string”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The choice Element
 XSD enables you to specify alternative options by using the choice element.
 The choice element allows only one of the elements contained in the group to
present within the parent element.
Syntax:
<choice id =”ID” maxOccurs = “nonNegativeInteger” | “unbounded”
minOccurs = “nonNegativeInteger”> </choice>
Example:
<xsd:schema xmIns:xsd = “http://www.w3.org/2001/XMLSchema”>
<xsd: element name = “CUSTOMER” type = “custtype”/>
<xsd: complexType name = “custtype”>
<xsd: sequence>
<xsd: group ref = “custname”/>
<xsd: element name = “ADDRESS” type = “addtype”/>
</xsd:sequence>
</xsd:complexType>
<xsd: complexType name = “addtype”>
<xsd: choice>
<xsd: element name = “RESIDENCE” type = “xsd: string”/>
<xsd: element name = “OFFICE” type = “xsd: string”/> </xsd: choice>
</xsd: complexType>
<xsd: group name = “custname”> <xsd: sequence>
<xsd: element name = “FIRSTNAME” type = “xsd: string”/>
<xsd: element name = “LASTNAME” type = “xsd: string”/>
</xsd:sequence>
</xsd:group>
</xsd:schema>

The following XML document conforms to the preceding schema:


<?xmI version = “1.0”?>
<CUSTOMER>
<FIRSTNAME> sam
</FIRSTNAME>
<LASTNAME> Peterson
</LASTNAME> <ADDRESS>
<RESIDENCE>10, LIONS STREET, BOSTON
</RESIDENCE>
</ADDRESS>
</CUSTOMER>
The all Element
 The all element enables you to use the child elements in any order as against
the sequence element, which specifies the order in which the child elements
must appear.
Syntax:
<all maxOccurs = “positiveInteger” minOccurs = “0|1”> </all>
 The following code contains the declaration for the EMPLOYEE:
Example:
<xsd:schema xmIns:xsd = http://www.w3.org/2001/XMLSchema>
<xsd:element name = “EMPLOYEE” type = “emptype” />
xsd:complexType name = “emptype”> <xsd:all>
<xsd:element name = “FIRSTNAME” type = “xsd:string”/>
<xsd:element name = “LASTNAME” type = “xsd:string”/>
<xsd:element name = “DESIG” type = “xsd:string”/>
<xsd:element name = “DEPARTMENT” type = “xsd:string”/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
The attributeGroup Element
 An XML schema also enables you to group attributes that can be reused with
different elements.
 You can group attributes by using the attributeGroup element.
Syntax:
<attributeGroup
attribute 1
attribute 2
:
</attributeGroup>
 In the syntax, attribute1, attribute2 are the declaration statements of the
various attributes that are to be grouped using the atributeGroup element.
<xsd:schema xmIns:xsd = http://www.w3.org/2001/XMLSchema>
<xsd:element name = “EMPLOYEE” type = “emptype” />
xsd:complexType name = “emptype”> <xsd:all>
<xsd: group ref = “empname”/>
<xsd:attributeGroup ref = “depdesig”>
</xsd:complexType>
<xsd: group name = “empname”>
<xsd: sequence>
<xsd: element name = “FIRSTNAME” type = “xsd: string”/>
<xsd: element name = “LASTNAME” type = “xsd: string”/>
</xsd:sequence> </xsd:group>
<xsd:attributeGroup name = “depdesig”>
<xsd: element name = “DEPARTMENT” type = “xsd: string”/>
<xsd: element name = “DESIGNATION” type = “xsd: string”/>
</xsd: attributeGroup>
</xsd:schema>
You can use the elements and attributes declared in the preceding schema as
follows:
<?xml version = “1.0”?>
<EMPLOYEE DEPARTMENT = “MKtg” DESIGNATION = “Mgr”>
<FIRSTNAME> james </FIRSTNAME>
<LASTNAME> Wallace
</LASTNAME>
</EMPLOYEE>
Grouping Elements and Attributes in an XML Document
 You have learned about various elements used for grouping user-defined
elements and Attributes.
 Now, let’s group the elements and attributes of an XML document.
To accomplish the given task, you need to perform the following tasks:
 Identify elements and attributes required to store data
 Identify the elements and attributes that need to be grouped
 Identify a mechanism for grouping elements and attributes in a schema
 Declare a group of elements and attributes in a schema
 Create an XML document to store data

Element Required for Storing Customer Details

Element Description
CUSTOMERDATA Acts as the root for all other elements that are used in the
XML document.
CUSTOMER Represents the details (first name, last name, and contact
information) for each customer.
FIRSTNAME Represent the first name of a customer.
LASTNAME Represent the last name of a customer
CONTACTINFO Represents either residential or official contact
information for a customer.
RESIDENCE Represents the
ADDRESS Represents the address of either the residence or the
office of a customer. This element will be used as the child
element of the RESIDENCE and OFFICE elements.
OFFICE Represents the official contact information for a
customer. This element will be used as a child element of
CONTACTINFO.
PHONE Represents the phone number of the residence or the
office for a customer. This element will be used as a child
element of CONTACTINFO.

<xsd:schema xmlns:xsd = “http://www.w3.org/2001/XMLSchema”>


<xsd: element name = “CUSTOMERDATA” type = “cdata”/>
<xsd: complexType name = “cdata”>
<xsd: sequence>
<xsd: element name = “CUSTOMER” type = “custtype”/>
</xsd: sequence>
</xsd:complexType>
<xsd: complexType name = “custname”>
<xsd:sequence>
<xsd: element name =”FIRSTNAME” type = “xsd:string”/>
<xsd: element name =”LASTNAME” type = “xsd:string”/>
<xsd: element name =”CONTACTINFO” type = “contacttype”/>
</xsd:sequence>
<xsd: attribute name = “CUSTOMERID” type = “xsd: string” use =
“required”/>
</xsd: complexType>
<xsd: complexType name = “contacttype>
<xsd: choice>
<xsd: element name = “RESIDENCE” type = “addtype”/>
<xsd: element name = “OFFICE” type = “addtype/>
<xsd:choice>
</xsd:complextype>
<xsd:complexType name = “addtype”>
<xsd: group ref = “ADDPHONE”/>
<xsd: complexType>
<xsd: group name = “ADDPHONE”>
<xsd: sequence>
<xsd:element name =”ADDRESS” type = “xsd:string”/>
<xsd:element name =”PHONE” type = “xsd:string”/>
</xsd:sequence>
</xsd:group>
</xsd:schema>
The following XML document conforms to the preceding schema:
<?xmI version = “1.0”?>
<CUSTOMERDATA>
<CUSTOMER CUSTOMERID = “C001”>
<FIRSTNAME> steve
</FIRSTNAME>
<LASTNAME> shaw
</LASTNAME>
<CONTACTINFO>
<RESIDENCE><ADDRESS>15, LIONS STREET, BOSTON </ADDRESS>
<PHONE> 172 – 693 – 1146 </PHONE></RESIDENCE>
</CONTACTINFO>
</CUSTOMERDATA>
XML STYLESHEETS
Introduction
 Processing an XML document to display it on different media, such as web
browsers, e-mail systems, pagers, and cellular phones, is known as rendering.
 Rendering instructs a medium how to present the data in an XML document.
 XML focuses on the data of a document.
 To render the data stored in an XML document, you need to use a special tool.
This tool is called a style sheet.
Style sheets
 A style sheet is a document that contains the formatting rules for one or more
XML documents.
 It contains the code that instructs the Web browser how to translate the format
of the source document into a customized format.
 You can use a style sheet to:
1. Change the format of an XML document.
2. Describe how to present the transformed information.
Changing the format
 This task is performed for the purpose of rendering an XML document to a
specific medium.
Changing format of an XML document might include:
 Generating constant text, such as labels and headings
 Specifying filters for extracting the required data
 Changing the sequence of elements and attributes
 Sorting the content
 Performing complex transformations that compute values based on the
existing content
Describing the presentation
 Describe the various instructions for presenting the transformed information
in a specific medium.
 Description about the presentation includes specifying instructions about:
1. Screen or page layout.
2. Assignment of transformed content into lists and paragraphs.
3. Specification of properties, such as spacing, margins, alignment, and fonts.
 The two types of style sheets used to format XML documents Cascading Style
Sheets(CSS) and eXtensible Style Sheet language Transformations(XSLT) .
Cascading style sheets
 A cascading style sheet(CSS) is a type of style sheet that provides a simple
mechanism for adding styles to an XML or HTML document.
 A CSS is a text file containing one or more rules or definitions for the style
characteristics of a particular element.
 It controls the formatting of tags in XML and HTML documents.
Creating a Style Sheet
 A CSS allows you to format the contents of a document by specifying element
names followed by the formatting instructions for the elements.
 Before creating a CSS, you need to identify the elements that are to be
displayed.
After identifying the elements, you can create the style by using the following
syntax:
elementname {
Property1: value;
Property2: value;
}
 The arguments property1, property2 specify names, such as font-size, font-
family, color, and font-weight.
 The value argument specifies different values for a property name. These
values called property values.
Consider an XML document:
<FILM> sixth sense</FILM>
<ACTOR> BRUCE WILLIES </ACTOR>

Display the film title in red color type this format:

FILM {COLOR:RED}

Set the same properties for more than one element:

FILM, ACTOR {COLOR : RED}

Applying a CSS to an XML Document


 To apply the formatting specifications of a CSS file to the data in an XML
document, you need to associate the CSS file with the XML document.
Syntax:
<?xmI-stylesheet type = “text/CSS” href = “path-name”?>
 The entire statement in the preceding syntax is called the Style sheet
processing instruction.
 In the syntax:
1. Xml-stylesheet instructs the browser that the XML document uses a
style sheet.
2. Type specifies the type of formatting used.
3. href specifies the name of the CSS file is used for formatting the XML
document. If the CSS file is stored in the same folder as that of the
XML document, then you need to only specify the name of the CSS
file. If they are stored in different folders, then you need to specify the
entire path of the CSS file.
Using a CSS for Formatting an XML Document
 You have learned how to create and apply a CSS.
 Now, let’s create a CSS to format an XML document.
The product details need to be displayed in a browser in the following format:
 The price per unit, description, and quantity on hand for each product should
be displayed in teal with a font size of 10 pts.
 The name of product must be display in red with a font size of 20 pts. It should
be in bold.
 All details must be displayed in Arial.
In the scenario of Cyber Shoppe, you need to display the following elements:
 PRODUCTNAME
 PRICE
 DESCRIPTION
 QUANTITY
< ? xml version = “1.0”>
<?xml : stylesheet type = “text/css” href =”product.css”?>
<PRODUCTDATA> <PRODUCT PRODID = “P001” CATEGORY = “TOY”>
<PRODUCTNAME> Mini Bus </PRODUCTNAME>
<DESCRIPTION> This is a toy for children aged 4 and above </DESCRIPTION>
<PRICE> 75 </PRICE>
<QUANTITY> 54 </QUANTITY>
</PRODUCT>
<PRODUCT PRODID =”P002” CATEGORY = “TOY”>
<PRODUCTNAME> Barbie Doll </PRODUCTNAME>
<DESCRIPTION> This toy is for children in the age group of 5-10
</DESCRIPTION>
<PRICE> 20 </PRICE>
<QUANTITY> 200 </QUANTITY>
</PRODUCT>
</PRODUCTDATA>
eXtensible Style Sheet Language
 CSS does not support operations, reordering and sorting elements, formatting
the content based on a condition, and displaying selective elements.
 To help you perform such operations, XML supports another style sheet
language called eXtensible Style Sheet Language (XSL).
Components of XSL
 XSL Transformations (XSLT) :
An XML-based language that allows you to transform an XML document into
another XML document. XSLT contains instructions for specifying how an
XML document is to be transformed.
 XML path (XPath) :
A language that allows you to access different parts of an XML document,
such as elements and attributes.
 Formatting objects :
Objects that specify how the data is to be displayed.

Comparison between XSLT and CSS


 XSLT is an application of XML. Therefore it follows the XML syntax,
 Whereas CSS has its own syntax. XML and CSS can co-exist because they are
for different needs.
Differences between CSS and XSLT
CSS XSLT
Is simple to use and is suitable for simple Is complex to use
documents
Cannot recorder, add, delete, or perform Can recorder, add, or delete elements
operations on elements because it is aware of the structure of
the XML document
Does not offer access to non-elements. Allows access to and manipulate the
comments
Uses less memory because it cannot Uses more memory and processor power
record a document and therefore, does because reordering, adding, deleting,
not need to build a tree representation and manipulating elements require a
of the document tree representation of document in the
memory
Uses a syntax that is different from XML Uses the same syntax as that of XML
Working of the XSLT Processor
 The XSLT processor comes packaged along with the Microsoft XML
(MSXML) parser.
 XSLT is an application of XML.
 The MSXML parser parses the XSLT style sheet and creates a tree structure
based on the elements and attribute used in an XSLT document. This tree is
known as the XSLT tree.
 The XSLT processor component of the MSXML parser takes the
transformation information contained in the XSLT style sheet and applies it to
the data retrieved from the source document.
 The XSLT processor component builds a resultant tree structure known as
result tree.
Depicts the working of the XSLT processor.

MSXML parser
XSLT style sheet XSLT tree

XSLT
XSLT Result tree
processor
Processor
pppppppp

XML document Source tree

XSLT Elements
 XSLT provides a number of elements for selecting and formatting data.
1. Stylesheet
2. Value-of
3. For-each
4. Sort
5. Text
The stylesheet Element
 The XSLT style sheet contains instructions for transforming XML documents.
 Therefore declaration is added in the XSLT file to instruct the browser that the
file is a style sheet file this declaration is known as style sheet declaration.
Syntax:
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSLT/Transform”
version = “1.0”>
 The stylesheet is the root element for all XSLT stylesheets.
 The xsl prefix contains a reference to the namespace URI for XSLT.
The value-of Element
 The value-of element displays the value of the specified element or attribute.
Syntax:
<xsl:value-of select = “elementname/attributename” />
 The value-of is an empty element, so you must include the “/” character before
the closing angular bracket of the tag.
 For Example the value-of element to display the value of the
PRODUCTNAME element:
<xsl:value-of select = “@CATEGORY”>
 If you want to display the value of an attribute, use the “@” symbol as a
prefix.
<xsl:value-of select = “@CATEGORY”>

The for-each Element


 The for-each element instructs the XSLT processor to process the information
for each instance of the specified pattern.
Syntax:
<xsl:for-each select = “pattern”>
[action to be performed]
</xsl:for-each>

 In the preceding syntax the pattern can be any one of the following:
1. Element
2. Parent/child
3. Ancestor//child
Different patterns of the for-each Element
PATTERNS SYNTAX EXPLANATION
Element <xsl:for-each select = Performs the specified action for
“PRODUCT”> each occurrence of the PRODUCT
element
Parent/child <xsl:for-each select = Performs the specified action for
“PRODUCTDATA/PRODUCT” each occurrence of the PRODUCT
> element, having the
PRODUCTDATA element as its
parents
Ancestor//child <xsl: for-each match = Applies the action to the element
“PRODUCT//BRAND”> BRAND, having the PRODUCT
element as its ancestor

Example:
<xsl : for-each select = “PRODUCTDATA / PRODUCT”>
<font color = “blue”> <xsl : value-of select = “PRODUCTNAME”>
</font> <xsl:value-of select = “PRICE”/>
<xsl:for-each>
The sort Element
 XSLT provides the sort element for sorting data based on values assigned to
elements and attributes.
 The sort element does not have any child elements.
Syntax:
<xsl:sort select = “expression” order = “ascending | descendir.g”
Case-order = “upper-first | lower-first”
Data-type = “text|number|qname”/>
Attribute of the sort Element
select expression Represents the element name.
Order Ascending Represents the sort order. The default
descending sort order is ascending.
Case-order Upper-first lower-first Uppercase letters come before lowercase
letters or vice versa
Data-type Text The data type can be a number, a
Number fragment of text, or a user-defined data
Qname type. The default data type is text.

Example:
<xsl:for-each select =”PRODUCT”>
<xsl:sort select =”PRICE” data-type=”number” order =”ascending”/>
<xsl:value-of select = “PRODUCTNAME”/>
<xsl:value-of select =”COVER_COLOR”/>
</xsl:for-each>
The text Element
 The text element allows you to generate constant texten the output
 This element can be used to display labels
Syntax
<xsl:text> product Name : </xsl:text>
Example:
<xsl:stylesheet xmlns:xsl = “http://www.w3.org/1999/XSL/Transform” version
=”1.0”>
<xsl:template match = “/”>
<xsl:for-each select =”PRODUCTDATA/PRODUCT”>
<xsl:sort select =”PRICE” data-type=”number” order=”descending”/>
<xsl:text> Product Id: </xsl:text>
<xsl:value-of select =”@PRODID”/>
XSLT Template Rules
 A template rule describes how an XML element and its contents are converted
into a format that can be displayed in the browser.
 XSLT allows you to define template rules for the output.
A template rule consists
 A pattern that identifies an element in an XML document.
 Action or processing code that details the transformation and rendering of the
resulting element.
The template Element
 The template element is used to define a template for the desired output.
Syntax
<xsl:template match = “pattern”>
[action to be taken]
</xsl:template>
Different values of pattern:
Pattern example
/ <xsl:template match = “/”>
. <xsl:template match = “.”>
element1 <xsl:template match = “customer”>
element1 | <xsl:template match = “firstname |
element2|…. lastname
element <xsl:template match =
[@attribute] “customer[@custid]”
Element <xsl:templatematch=
[@attribute = “customer[@custid=’1’]”>
‘value’]

Parent/child <xsl:templatematch=
“customer/firstname”>
Ancestor//child <xsl:templatematch=
“customerdetails//address”>

The apply- templates Element


 The apply-templates element is used to instruct the XSLT processor to find an
appropriate template and perform the specified tasks on each selected element.
Syntax:
<xsl:apply-templates [select = “pattern”]>
The simplest way of using template and apply-templates elements is given in code:
<xsl:stylesheet xmlns:xsl = “http://www.w3.org/1999/XSL/
Transform” version = “1.0”>
<xsl:template match = “/”>
<xsl:apply-templates />
</xsl:template>
Default Template
 The XSLT processor processes all the elements in the hierarchy of an XML
document to produce an output.
Style sheet:
<xsl:apply-templates select = “PRODUCTDATA/PRODUCT”/>

Default template rule


<xsl:template match = “DESCRIPTON”>
<font color = “red”>
<xsl:apply-templates / >
</font>
</xsl:template>
In the preceding code, the statement, <xsl:apply-template select = “DESCRIPTION”>
Viewing Selective Details
 To view the selective details from an XML document,
 You need to associate the XML document with the XSLT file that renders
data.
Syntax:
<?xml-stylesheet type = “text/xsl” href = “path-name”?>
In the preceding syntax:
 Xml-stylesheet is the style sheet processing instruction
 Type specifies the type of formatting used, the type attribute is set to “xsl.”
 href specifies the path and the name of the XSLT file used for formatting the
XML document.
Applying an XSLT style sheet to an XML Document
 You have learned about elements and template rules of the XSLT style sheet.
XSLT elements to extract and format the required data:
 Stylesheet: To specify the style sheet declaration
 For-each: To display the product ID
 Value-of: To display the values of PRODID
 Sort: To sort the element and attribute containing product details based on
product ID
 Text: To display the labels for various elements and attributes
 Template: to use as the parent element of the for-each element
Formatting Data Based on Conditions
 Format the data in an XML document based on specific conditions.
The if Element
The if element of XSLT provides a simple if-then construct.
Syntax:
<xsl:if test = “condition”>
[action to be performed if the condition is true]
</xsl:if>
Code to display the names of products priced at higher than $100:
<xsl:if test = “PRICE [ &gt: 100]”>
<xsl:value-of select = “PRODUCTNAME”/>
</xsl:if>
</xsl:template>
</xsl:template>
</xsl:stylesheet>
Displaying Data in a Tabular Format
 Information about the way in which XML data must appear in a browser is
specified in either CSS or XSLT style sheets.
 HTML is a markup language that is used to display information to a user in a
browser
 In contrast to XML, HTML is used primarily for data display.
 Therefore, you can combine the features of HTML and XSLT to display the
data of an XML document in a customized format.
Using HTML Elements in an XSLT Style Sheet
 In order to use HTML tags in an XSLT style sheet, you must write the HTML
code to display the data in the desired format
HTML Element Used in XSLT
TABLE Used as a container for all other tags used to specify the appearance of
data in a table.
THEAD Used to specify headings for a table.
TBODY Used as a parent for the TR and TD elements
TR Used to represent a row in a table. It acts as a container for the TH and
TD elements
TH Used to add column headings
TD Used to specify the data to be displayed in columns

Creating a comma-separated list of values


 When you display data in tabular format, sometimes you might need to list
multiple elements in a single column.
 In order to create a comma-separated list of values.
How these template rules will works.
 The XSLT processor starts processing from the root node.
 The for-each construct instructs the processor to process each instance of the
BOOK element that is present within the BOOKS element.
 The for-each construct contains the apply-templates statement with the select
attribute set to AUTHOR.
 Because a template rule for the AUTHOR element is specified.
 The xsl:if element checks whether there are more instances of the AUTHOR
element for a BOOK.
 After processing the template rule, the XSLT processor containues with the
for-each construct and proceeds to the next BOOK element in the XML
document
Creating a style sheet to Display Data in a Tabular Format
 You have learned about the use of HTML and XSLT elements in combination
to display data in a tabular form.
 The details of the book that are available for sale at CyberShoppe are stored in
an XML document.
In the preceding scenario, you need to display the following data
 BOOKID
 TITLE
 RATE
 AUTHOR
 FIRSTNAME
 LASTNAME
The following HTML tags in an XSTL style sheet to display
 TABLE
 TR
 TH
 TD
Nested Style Sheets
While programming XML applications, you might want to include the information
contained in one XSLT style sheet in another XSLT style sheet
The import Element
The import element allows you to reuse the formatting instructions specified in one
XSLT style sheet in another XSLT xtyle sheet.
The general form of the import element is:
<xsl:import href = “location”/>
The import element is atop-level element and must appear immediately after the
stylesheet element.

You might also like