You are on page 1of 233

TUTORIAL 1

XP

CREATING AN XML DOCUMENT

INTRODUCING XML

XP

XML stands for Extensible Markup Language. A


markup language specifies the structure and
content of a document.
Because it is extensible, XML can be used to
create a wide variety of document types.

INTRODUCING XML

XP

XML is a subset of the Standard Generalized


Markup Language (SGML) which was introduced
in the 1980s. SGML is very complex and can be
costly. These reasons led to the creation of
Hypertext Markup Language (HTML), a more
easily used markup language. XML can be seen as
sitting between SGML and HTML easier to
learn than SGML, but more robust than HTML.
3

THE LIMITS OF HTML

XP

HTML was designed for formatting text on a Web page. It


was not designed for dealing with the content of a Web
page. Additional features have been added to HTML, but
they do not solve data description or cataloging issues in
an HTML document.
Because HTML is not extensible, it cannot be modified to
meet specific needs. Browser developers have added
features making HTML more robust, but this has resulted
in a confusing mix of different HTML standards.
4

THE LIMITS OF HTML

XP

HTML cannot be applied consistently. Different


browsers require different standards making the
final document appear differently on one browser
compared with another.

XP
THE 10 PRIMARY XML DESIGN
GOALS

1.
2.
3.
4.

XML must be easily usable over the Internet


XML must support a wide variety of applications
XML must be compatible with SGML
It must be easy to write programs that process
XML documents
5. The number of optional features in XML must be
kept to a minimum, ideally zero

XP
THE 10 PRIMARY XML DESIGN
GOALS CONTINUED

6. XML documents should be clear and easily


understood by nonprogrammers
7. The XML design should be prepared quickly
8. The design of XML must be exact and concise
9. XML documents must be easy to create
10. Terseness in XML markup is of minimum
importance

XML VOCABULARIES

XP

XP
WELL-FORMED AND VALID XML
DOCUMENTS

An XML document is well-formed if it contains


no syntax errors and fulfills all of the
specifications for XML code as defined by the
W3C.
An XML document is valid if it is well-formed
and also satisfies the rules laid out in the DTD or
schema attached to the document.

THE STRUCTURE OF AN XMLXP


DOCUMENT
XML documents consist of three parts
The prolog
The document body
The epilog
The prolog is optional and provides information
about the document itself
10

THE STRUCTURE OF AN XMLXP


DOCUMENT
The document body contains the documents
content in a hierarchical tree structure.
The epilog is also optional and contains any final
comments or processing instructions.

11

THE STRUCTURE OF AN XMLXP


DOCUMENT: CREATING THE
PROLOG
The prolog consists of four parts in the following
order:
XML declaration
Miscellaneous statements or comments
Processing instructions
Document type declaration

12

THE STRUCTURE OF AN XMLXP


DOCUMENT:
THE XML DECLARATION
The XML declaration is always the first line of code in an
XML document. It tells the processor what follows is
written using XML. It can also provide any information
about how the parser should interpret the code.
The complete syntax is:
<?xml version=version number encoding=encoding
type standalone=yes | no ?>
A sample declaration might look like this:
<?xml version=1.0 encoding=UTF-8
standalone=yes ?>
13

THE STRUCTURE OF AN XMLXP


DOCUMENT:
INSERTING COMMENTS
Comments or miscellaneous statements go after
the declaration. Comments may appear anywhere
after the declaration.
The syntax for comments is:
<!- - comment text - ->
This is the same syntax for HTML comments
14

XP

ELEMENTS
Elements are the basic building blocks of XML
files.
Elements contain an opening tag and a closing tag
Content is stored between tags

15

ELEMENTS

XP

A closed element, has the following syntax:


<element_name>Content</element_name>
Example:
<Artist>Miles Davis</Artist>

16

ELEMENT

XP

Element names are case sensitive


Elements can be nested, as follows:
<tracks>Kind of Blue
<track>So What ((:22)</track>
<track>Blue in Green (5:37)</track>
</tracks>
17

XP

ELEMENTS
Nested elements are called child elements.
Elements must be nested correctly. Child elements
must be enclosed within their parent elements.

18

XP

ELEMENTS AND ATTRIBUTES


All elements must be nested within a single
document or root element. There can be only one
root element.
An open or empty element is an element that
contains no content. They can be used to mark
sections of the document for the XML parser.

19

XP

WORKING WITH ATTRIBUTES


An attribute is a feature or characteristic of an
element. Attributes are text strings and must be
placed in single or double quotes. The syntax is:
<element_name attribute=value>
</element_name>

20

ELEMENTS AND ATTRIBUTES: XP


ADDING ELEMENTS TO THE
JAZZ.XML FILE
This figure shows the revised document

document
elements

{
21

CHARACTER REFERENCES

XP

Special characters, such as the symbol for the


British pound, can be inserted into your XML
document by using a character reference. The
syntax is:
&#nnn;

22

CHARACTER REFERENCES

XP

Character is a character reference number or name


from the ISO/IEC character set.
Character references in XML are the same as in
HTML.

23

CHARACTER REFERENCES

XP

This figure shows commonly used character reference numbers

24

CHARACTER REFERENCES

XP

This figure shows the revised Jazz.XML file

character
reference

25

PARSED CHARACTER DATA

XP

Parsed character data, or pcdata consists of all those


characters that XML treats as parts of the code of XML
document
The XML declaration
The opening and closing tags of an element
Empty element tags
Character or entity references
Comments

26

XP

CDATA SECTIONS
A CDATA section is a large block of text the XML
processor will interpret only as text.
The syntax to create a CDATA section is:
<! [CDATA [
Text Block
] ]>

27

CDATA SECTIONS

XP

In this example, a CDATA section stores several HTML


tags within an element named HTMLCODE:
<htmlcode>
<![CDATA[
<h1>The Jazz Warehouse</h1>
<h2>Your Online Store for Jazz Music</h2>
] ]>
</htmlcode>
28

CDATA SECTIONS

XP

This figure shows the revised Jazz.XML file

CDATA section

29

XP

PARSING AN XML DOCUMENT

30

XP
DISPLAYING AN XML DOCUMENT
IN A WEB BROWSER
XML documents can be opened in Internet Explorer or in
Netscape Navigator.
If there are no syntax errors. IE will display the
documents contents in an expandable/collapsible outline
format including all markup tags.
Netscape will display the contents but neither the tags nor
the nested elements.

31

XP
DISPLAYING AN XML DOCUMENT
IN A WEB BROWSER

To display the Jazz.xml file in a Web browser:


1. Start the browser and open the Jazz.xml file
located in the tutorial.01x/tutorial folder of
your Data Disk.
2. Click the minus (-) symbols.
3. Click the resulting plus (+) symbols.

32

XP
DISPLAYING AN XML DOCUMENT
IN A WEB BROWSER

33

XP

LINKING TO A STYLE SHEET


Link the XML document to a style sheet to format
the document. The XML processor will combine
the style sheet with the XML document and apply
any formatting codes defined in the style sheet to
display a formatted document.
There are two main style sheet languages used
with XML:
Cascading Style Sheets (CSS) and Extensible
Style Sheets (XSL)
34

XP

LINKING TO A STYLE SHEET


There are some important benefits to using style
sheets:
By separating content from format, you can
concentrate on the appearance of the document
Different style sheets can be applied to the same
XML document
Any style sheet changes will be automatically
reflected in any Web page based upon the style
sheet
35

APPLYING A STYLE TO AN
ELEMENT

XP

To apply a style sheet to a document, use the following


syntax:
selector {attribute1:value1; attribute2:value2; }
selector is an element (or set of elements) from the XML
document.
attribute and value are the style attributes and attribute
values to be applied to the document.

36

APPLYING A STYLE TO AN
ELEMENT

XP

For example:
artist {color:red; font-weight:bold}
will display the text of the artist element in a red
boldface type.

37

XP

CREATING PROCESSING
INSTRUCTIONS
The link from the XML document to a style sheet
is created using a processing statement.
A processing instruction is a command that gives
instructions to the XML parser.

38

CREATING PROCESSING
INSTRUCTIONS

XP

For example:
<?xml-stylesheet type=style href=sheet ?>
Style is the type of style sheet to access and sheet
is the name and location of the style sheet.

39

THE JW.CSS STYLE SHEET

XP

This figure shows the cascading style sheet stored in the jw.css file

40

LINKING TO THE JW.CSS


STYLE SHEET

XP

This figure shows how to link the JW.css style sheet to the Jazz.xml file

processing instruction to
access the jw.css style sheet
41

THE JAZZ.XML DOCUMENT XP


FORMATTED WITH THE JW.CSS
STYLE SHEET
This figure shows the
formatted jazz.xml file

42

TUTORIAL 2

XP

WORKING WITH NAMESPACES

43

COMBINING XML
VOCABULARIES

XP

A document that combines several vocabularies is known


as a compound document

44

XP

WORKING WITH NAMESPACES


Name collision occurs when elements from two or
more documents share the same name.
Name collision is not a problem if you are not
concerned with validation. The document content
only needs to be well-formed.
However, name collision will keep a document
from being validated.
45

NAME COLLISION

XP

This figure shows name collision

46

DECLARING A NAMESPACE

XP

A namespace is a defined collection of element


and attribute names.
Names that belong to the same namespace must be
unique. Elements can share the same name if they
reside in different namespaces.
Namespaces must be declared before they can be
used.
47

DECLARING A NAMESPACE

XP

A namespace can be declared in the prolog or as an


element attribute. The syntax for an attribute used to
declare a namespace in the prolog is:
xmlns:prefix=URI
Where URI is a Uniform Resource Identifier that assigns
a unique name to the namespace, and prefix is a string of
letters that associates each element or attribute in the
document with the declared namespace.
48

DECLARING A NAMESPACE

XP

For example,
<mode1 xmlns:mod=http://jacksonelect.com/models>>
Declares a namespace with the prefix mod and the URI
http://jacksonelect.com/models
The URI is not a Web address. A URI identifies a physical
or an abstract resource.
49

XP

URIs, URLs, AND URNs


A physical resource is a resource one can access
and work with such as a file, a Web page, or an email address. A URL is one type of URI.
An abstract resource is one that doesnt have any
physical existence, the URI is used as an identifier
or an ID.

50

TUTORIAL 3

XP

VALIDATING AN XML DOCUMENT

51

XP

CREATING A VALID DOCUMENT


You validate documents to make certain
necessary elements are never omitted.
For example, each customer order should
include a customer name, address, and
phone number.

52

XP

CREATING A VALID DOCUMENT


Some elements and attributes may be optional, for
example an e-mail address.
An XML document can be validated using either
DTDs (Document Type Definitions) or schemas.

53

CUSTOMER INFORMATION
COLLECTED BY KRISTEN

XP

This figure shows customer information collected by Kristen

54

XP
THE STRUCTURE OF KRISTENS
DOCUMENT
This figure shows the overall structure of Kristens document

55

DECLARING A DTD

XP

A DTD can be used to:


Ensure all required elements are present in the
document
Prevent undefined elements from being used
Enforce a specific data structure
Specify the use of attributes and define their
possible values
Define default values for attributes
Describe how the parser should access non-XML
or non-textual content
56

XP

DECLARING A DTD
There can only be one DTD per XML document.
A document type definition is a collection of
rules or declarations that define the content and
structure of the document.
A document type declaration attaches those
rules to the documents content.

57

DECLARING A DTD

XP

You create a DTD by first entering a document


type declaration into your XML document.
DTD in this tutorial will refer to document type
definition and not the declaration.
While there can only be one DTD, it can be
divided into two parts: an internal subset and an
external subset.
58

DECLARING A DTD

XP

An internal subset is declarations placed in


the same file as the document content.
An external subset is located in a separate
file.

59

XP

DECLARING A DTD
The DOCTYPE declaration for an internal subset is:
<!DOCTYPE root
[
declarations
]>
Where root is the name of the documents root element,
and declarations are the statements that comprise the
DTD.

60

DECLARING A DTD

XP

The DOCTYPE declaration for external


subsets can take two forms: one that uses a
SYSTEM location and one that uses a
PUBLIC location.
The syntax is:
<!DOCTYPE root SYSTEM uri> or
<!DOCTYPE root PUBLIC id uri>
61

XP

DECLARING A DTD
Here, root is the documents root element,
identifier is a text string that tells an application
how to locate the external subset, and uri is the
location and filename of the external subset.
Use the PUBLIC location form when the DTD
needs to be limited to an internal system or
when the XML document is part of an old
SGML application.

62

DECLARING A DTD

XP

The SYSTEM location form specifies the


name and location of the external subset
through the uri value.
Unless your application requires a public
identifier, you should use the SYSTEM
location form.

63

DECLARING A DTD

XP

A DOCTYPE declaration can indicate both an external and an


internal subset. The syntax is:
<!DOCTYPE root SYSTEM URI
[
declarations
]>
or
<!DOCTYPE root PUBLIC id URL
[
declarations
]>
64

DECLARING A DTD

XP

If you place the DTD within the document, it is


easier to compare the DTD to the documents
content. However, the real power of XML
comes from an external DTD that can be
shared among many documents written by
different authors.

65

DECLARING A DTD

XP

If a document contains both an internal and


an external subset, the internal subset takes
precedence over the external subset if there is
a conflict between the two.
This way, the external subset would define
basic rules for all the documents, and the
internal subset would define those rules
specific to each document.
66

XP
COMBINING AN EXTERNAL AND
INTERNAL DTD SUBSET
This figure shows how to combine an external and an internal DTD subset

67

XP
WRITING THE DOCUMENT TYPE
DECLARATION
This figure shows how to insert an internal DTD subset

68

XP

DECLARING DOCUMENT
ELEMENTS
Every element used in the document must be
declared in the DTD for the document to be
valid.
An element type declaration specifies the
name of the element and indicates what kind
of content the element can contain.

69

DECLARING DOCUMENT
ELEMENTS

XP

The element declaration syntax is:


<!ELEMENT element content-model>
Where element is the element name and
content-model specifies what type of content
the element contains.

70

XP

DECLARING DOCUMENT
ELEMENTS
The element name is case sensitive.
DTDs define five different types of element content:
Any elements. No restrictions on the elements
content.
Empty elements. The element cannot store any
content.

71

XP

DECLARING DOCUMENT
ELEMENTS
#PCDATA. The element can only contain
parsed character data.
Elements. The element can only contain child
elements.
Mixed. The element contains both a text string
and child elements.

72

XP

TYPES OF ELEMENT CONTENT


ANY content: The declared element can store
any type of content. The syntax is:
<!ELEMENT element ANY>
EMPTY content: This is reserved for elements
that store no content. The syntax is:
<!ELEMENT element EMPTY>
73

XP

TYPES OF ELEMENT CONTENT


Parsed Character Data content: These elements
can only contain parsed character data. The
syntax is:
<!ELEMENT element (#PCDATA)>
The keyword #PCDATA stands for parsedcharacter data and is any well-formed text string.
74

XP

TYPES OF ELEMENT CONTENT


ELEMENT content.: The syntax for declaring
that elements contain only child elements is:
<!ELEMENT element (children)>
Where children is a list of child elements.

75

XP

TYPES OF ELEMENT CONTENT


The declaration <!ELEMENT customer
(phone)> indicates the customer element can
only have one child, named phone. You
cannot repeat the same child element more
than once with this declaration.

76

ELEMENT SEQUENCES AND XP


CHOICES
A sequence is a list f elements that follow a
defined order. The syntax is:
<!ELEMENT element (child1, child2, )>
The order of the child elements must match the
order defined in the element declaration. A
sequence can be applied to the same child
element.
77

ELEMENT SEQUENCES AND XP


CHOICES
Thus,
<!ELEMENT customer (name, phone,
email)>
indicates the customer element should
contain three child elements for each
customer.
78

ELEMENT SEQUENCES AND XP


CHOICES
Choice is the other way to list child elements
and presents a set of possible child elements.
The syntax is:
<!ELEMENT element (child1 | child2 | )>
where child1, child2, etc. are the possible
child elements of the parent element.
79

ELEMENT SEQUENCES AND XP


CHOICES
For example,
<!ELEMENT customer (name | company)>
This allows the customer element to contain
either the name element or the company
element. However, you cannot have both the
customer and the name child elements since the
choice model allows only one of the child
elements.
80

MODIFYING SYMBOLS

XP

Modifying symbols are symbols appended to


the content model to indicate the number of
occurrences of each element. There are three
modifying symbols:
a question mark (?), allow zero or one of the
item.
a plus sign (+), allow one or more of the item.
an asterisk (*), allow zero or more of the item.
81

MODIFYING SYMBOLS

XP

For example, <!ELEMENT customer


(customer+)> would allow the document to
contain one or more customer elements to be
placed within the customer element.
Modifying symbols can be applied within
sequences or choices. They can also modify
entire element sequences or choices by placing
the character immediately following the closing
parenthesis of the sequence or choice.
82

XP

MIXED CONTENT
Mixed content elements contain both character data and
child elements. The syntax is:
<!ELEMENT element (#PCDATA) | child1 | child2 |
)*>
This form applies the * modifying symbol to a choice of
character data or elements. Therefore, the parent element
can contain character data or any number of the specified
child elements, or it can contain no content at all.

83

MIXED CONTENT

XP

Because you cannot constrain the order in


which the child elements appear or control
the number of occurrences for each element,
it is better not to work with mixed content if
you want a tightly structured document.

84

XP

DECLARING ELEMENT
ATTRIBUTES
For a document to be valid, all the attributes
associated with elements must also be
declared. To enforce attribution properties,
you must add an attribute-list declaration to
the documents DTD.

85

ELEMENT ATTRIBUTES IN
KRISTENS DOCUMENT

XP

This figure shows element attributes in Kristen's document

86

XP

DECLARING ELEMENT
ATTRIBUTES
The attribute-list declaration :
Lists the names of all attributes associated
with a specific element
Specifies the data type of the attribute
Indicates whether the attribute is required
or optional
Provides a default value for the attribute, if
necessary

87

XP

DECLARING ELEMENT
ATTRIBUTES
The syntax to declare a list of attributes is:
<!ATTLIST element attribute1 type1 default1
attribute2 type2 default2
attribute3 type3 default3>
Where element is the name of the element
associated with the attributes, attribute is the name
of an attribute, type is the attributes data type, and
default indicates whether the attribute is required or
implied, and whether it has a fixed or default value.

88

DECLARING ELEMENT
ATTRIBUTES

XP

Attribute-list declaration can be placed


anywhere within the document type
declaration, although it is easier if they are
located adjacent to the declaration for the
element with which they are associated.

89

XP

WORKING WITH
ATTRIBUTE TYPES
While all attribute types are text strings, you can control
the type of text used with the attribute. There are three
general categories of attribute values:
CDATA
enumerated
Tokenized
CDATA types are the simplest form and can contain any
character except those reserved by XML.

Enumerated types are attributes that are limited to a set


of possible values.
90

WORKING WITH
ATTRIBUTE TYPES

XP

The general for of an enumerated type is:


attribute (value1 | value2 | value3 | )
For example, the following declaration:
customer custType (home | business )>
restricts CustType to either home or business
91

WORKING WITH
ATTRIBUTE TYPES

XP

Another type of enumerated attribute is notation. It


associates the value of the attribute with a <!
NOTATION> declaration located elsewhere in the
DTD. The notation provides information to the XML
parser about how to handle non-XML data.
Tokenized types are text strings that follow certain
rules for the format and content. The syntax is:
attribute token
92

WORKING WITH
ATTRIBUTE TYPES

XP

There are seven tokenized types. For example,


the ID token is used with attributes that require
unique values. For example, if a customer ID
needs to be unique, you may use the ID token:
customer custID ID
This ensures each customer will have a unique
ID.
93

ATTRIBUTE TYPES

XP

This figure shows the attribute types

94

ATTRIBUTE DEFAULTS

XP

The final part of an attribute declaration is the


attribute default. There are four possible defaults:
#REQUIRED: the attribute must appear with every
occurrence of the element.
#IMPLIED: The attribute is optional.
An optional default value: A validated XML parser
will supply the default value if one is not specified.
#FIXED: The attribute is optional but if one is
specified, it must match the default.
95

INSERTING ATTRIBUTE-LIST XP
DECLARATIONS
This figure the revised contents of the Orders.xml file

attribute declaration

96

WORKING WITH ENTITIES

XP

Entities are storage units for a documents


content. The most fundamental entity is the XML
document itself and is known as the document
entity. Entities can also refer to:
a text string
a DTD
an element or attribute declaration
an external file containing character or binary
data
97

XP

WORKING WITH ENTITIES


Entities can be declared in a DTD. How to
declare an entity depends on how it is
classified. There are three factors involved in
classifying entities:
The content of the entity
How the entity is constructed
Where the definition of the entity is located.

98

XP

GENERAL PARSED ENTITIES


General entities are declared in the DTD of a document. The
syntax is:
<!ENTITY entity value>
Where entity is the name assigned to the entity and value is
the general entitys value.
For example, an entity named DCT5Z can be created to
store a product description:
<!ENTITY DCT5Z (Topan Digital Camera 5 Mpx - zoom>
99

XP

GENERAL PARSED ENTITIES


After an entity is declared, it can be referenced
anywhere within the document.
<item>&DCT5Z;</item>
This is interpreted as
<item>Tapan Digital Camera 5 Mpx zoom</item>
100

XP

ENTITIES IN THE
ITEMS.DTD FILE
This figure shows the entities in the codestxt.dtd file

entity name

entity value

101

XP

PARAMETER ENTITIES
Parameter entities are used to store the content of a
DTD. For internal parameter entities, the syntax is:
<!ENTITY % entity value>
where entity is the name of the parameter entity and
value is a text string of the entitys value.
For external parameter entities, the syntax is:
<!ENTITY % entity SYSTEM uri>
where uri is the name assigned to the parameter entity.

102

XP

PARAMETER ENTITIES
Parameter entity references can only be placed
where a declaration would normally occur, such
as an internal or external DTD.
Parameter entities used with an internal DTD do
not offer any time or effort savings. However, an
external parameter entity can allow XML to use
more than one DTD per document by combining
declarations from multiple DTDs.

103

XP
USING PARAMETER ENTITIES TO
COMBINE MULTIPLE DTDS
This figure shows how to combine multiple DTDs using parameter entities

104

UNPARSED ENTITIES

XP

You need to create an unparsed entity in order


to reference binary data such as images or
video clips, or character data that is not well
formed. The unparsed entity includes
instructions for how the unparsed entity
should be treated.
A notation is declared that identifies a
resource to handle the unparsed data.
105

XP

UNPARSED ENTITIES
For example, to create a notation named audio that
points to an application Recorder.exe:
<!NOTATION jpeg SYSTEM paint.exe>
Once the notation has been declared, you then
declare an unparsed entity that instructs the XML
parser to associate the data to the notation.

106

XP

UNPARSED ENTITIES
For example, to take unparsed data in an audio file
and assign it to an unparsed entity named Theme:,
use the following:
<!ENTITY DCT5ZIMG SYSTEM dct5z.jpg
NDATA jpeg>
Here, the notation is the jpeg notation that points to
the paint.exe file. This declaration does not tell the
paint.exe application to run the file but simply
identifies for the XML parser what resource is able to
handle the unparsed data.

107

URIs, URLs, AND URNs

XP

A proposed type of URI is the URN or Universal


Resource Name. A URN is a persistent resource
identifier, meaning the user need only know the
name of a resource. An agency would then retrieve
a copy of the resource independent of its location.
URNs take the form:
urn:NID:NSS

108

XP
APPLYING A NAMESPACE TO AN
ELEMENT
Once it has been declared and its URI specified, the
namespace is applied to elements and attributes by inserting
the namespace prefix before each element name that belongs
to the namespace.
<prefix:element>
content
</prefix:element>
Here, prefix is the namespace prefix and element is the local
part of the element name.
109

XP
APPLYING A NAMESPACE TO AN
ELEMENT
Prefixed names are called qualified names and an element
name without a namespace prefix is called an unqualified
name.
Qualified names can be added to a document using code
entered directly into the document.
However, the more common way is to add the xmlns
attribute to an element.

110

XP
DECLARING A NAMESPACE AS
AN ELEMENT ATTRIBUTE

The syntax is:


xmlns:prefix=URI
Where prefix and URI are the prefix and URI for
the namespace.

111

XP
DECLARING A NAMESPACE AS
AN ELEMENT ATTRIBUTE
For example, the code:
<mod:model
xmlns:mod="http://jacksonelect.com/models">
<mod:title>Laser4C (PR205)</mod:title>
<mod:description>Entry level color laser
printer</mod:description>
<mod:type>color laser</mod:type>
<mod:ordered>320</mod:ordered>
<mod:parts list="chx201,fa100-5,eng005-2,cbx450V4,tn01-53" />
</mod:model>
112

XP
DECLARING A NAMESPACE AS
AN ELEMENT ATTRIBUTE

applies the namespace


http://jacksonelect.com/models namespace to the
model element and all of its child elements.
While the mod prefix was only added to the
model element name, the XML parser considers
the other elements parts of the model namespace
and they inherit the namespace.
113

XP
DECLARING A NAMESPACE AS
AN ELEMENT ATTRIBUTE

They are unqualified elements, though, because


they lack a namespace prefix.
Declaring a namespace by adding it as an attribute
of the documents root element places all elements
in the namespace.
All elements thus are children of the root element.
114

XP

DECLARING A DEFAULT
NAMESPACE
You can specify a default namespace by omitting
the prefix in the namespace declaration.
The element containing the namespace attribute
and all of its child elements are assumed to be part
of the default namespace.

115

USING NAMESPACES WITH XP


ATTRIBUTES
Attributes, like elements, can become qualified by
adding the namespace prefix to the attribute name.
For example,
<element xmlns=uri>
content
</element>

116

USING NAMESPACES WITH XP


ATTRIBUTES
No element may contain two attributes with the
same name.
No element may contain two qualified attribute
names with the same local part, pointing to
identical namespaces, even if the prefixes are
different.

117

ADDING A NAMESPACE TO A STYLE


XP
SHEET:
DECLARING A NAMESPACE
To declare a namespace in a style sheet, you add the
following rule to the style sheet file
@namespace prefix url(uri);
Where prefix is the namespace previx and uri is the URI of
the namespace
Example:
@namespace mod url(http://jacksonelect.com/models)
118

XP
APPLYING A NAMESPACE TO A
SELECTOR
Once youve declared a namespace in a style sheet, you can
associate selectors with that namespace using the syntax:
prefix|selector {attribute1:value1; attribute2:value2;}
For example:
mod|title {width: 150px}
You also can use the wildcard symbol (*) to apply a style to any
element within a namespace or to elements across different
namespaces
119

XP
DEFINING NAMESPACES WITH
THE ESCAPE CHARACTER
Not all browsers support the use of the @namespace rule
A proposal implement in the Internet Explorer browser
was to insert the backslash escape character before the
namespace prefix in CSS style sheets:
prefix\:selector {attribute1:value1; attribute2:value2;}
Browsers like Firefox, Opera, and Netscape do not support
this method with XML documents

120

DECLARING AND APPLYING AXP


NAMESPACE IN A STYLE SHEET

To declare a namespace in a CSS style sheet, add the following rule


before any style declarations:
@namespace prefix url(uri);
where prefix is the namespace prefix and uri is the namespace URI. If no
prefix is specified, the namespace URI is the default namespace for
selectors in the style sheet.
To apply a namespace to a selector, use the form
prefix|selector {attribute1:value1; attribute2:value2; ...}
where prefix is the namespace prefix and selector is a selector for an
element or group of elements in the document.
For Internet Explorer browsers, use the following form to apply a
namespace to a selector:
prefix\:selector {attribute1:value1; attribute2:value2; ...}
121

COMBINING STANDARD
VOCABULARIES

XP

Standard vocabularies may be combined within single


documents

122

XP

CONVERTING HTML TO XHTML


Use your text editor to open the reptxt.htm file from the
tutorial.02x/tutorial folder. Enter your name and the date
in the comment section at the top of the document. Save
the file as report.htm.
Insert the following xml declaration as the very first line in
the file (above the comment section):
<?xml version="1.0" encoding="UTF-8"
standalone="yes" ?>
Add the following attribute to the opening <html> tag:
xmlns="http://www.w3.org/1999/xhtml"
123

XP

CONVERTING HTML TO XHTML

124

XP
ADDING THE ELEMENTS OF THE
PARTS VOCABULARY

Return to the order.xml file in your text editor.


Copy the parts element from the parts namespace, including all of the
elements and contents it contains.
Return to the report.htm file in your text editor and paste the copied
elements directly below the h2 heading Parts List.
Add the following attribute to the opening <html> tag:
xmlns:pa="http://jacksonelect.com/parts"
Below the link element that links the report.htm file to the report.css
style sheet, insert the following link element:
<link rel="stylesheet" href="parts.css" type="text/css" />
Save the changes and open the report.htm file in your Web browser
125

XP
ADDING THE ELEMENTS OF THE
PARTS VOCABULARY

126

DESCRIBING THE ITEMS


IN THE PARTS LIST

XP

Return to the report.htm file in your text editor.


Scroll down to the first title element in the parts namespace.
Directly after the opening <pa:title> tag, insert the text
<span>Title</span>
Directly after the opening <pa:description> tag in the next line,
insert the text
<span>Description</span>
Directly after the opening <pa:instock> tag in the following line,
insert the text
<span>Parts in Stock</span>
Repeat the previous 3 steps, as necessary
127

DESCRIBING THE ITEMS


IN THE PARTS LIST

XP

128

DESCRIBING THE ITEMS


IN THE PARTS LIST

XP

129

XP
ADDING ELEMENTS FROM THE
MODELS VOCABULARY
Return to the report.htm file in your text editor and add
the following namespace declaration to the opening <html>
tag:
xmlns:mod="http://jacksonelect.com/models"
Add the following link to the documents head:
<link rel="stylesheet" href="model.css"
type="text/css" />
In the table cell directly after the Title table heading, insert
the element
<mod:title>Laser4C (PR205)</mod:title>
130

XP
ADDING ELEMENTS FROM THE
MODELS VOCABULARY
In the table cell directly after the Description table heading,
insert the element
<mod:description>Entry level color laser
printer</mod:description>
In the table cell directly after the Type table heading, insert
the element
<mod:type>color laser</mod:type>
In the table cell directly after the Items to be Built table
heading, insert the element
<mod:ordered>320</mod:ordered>
131

XP
ADDING ELEMENTS FROM THE
MODELS VOCABULARY

132

TUTORIAL 4

XP

WORKING WITH SCHEMAS

133

XP

SCHEMAS
A schema is an XML document that defines
the content and structure of one or more XML
documents.
The XML document containing the content is
called the instance document.

134

COMPARING SCHEMAS
AND DTDS

XP

This figure compares schemas and DTDs

135

XP

SCHEMA VOCABULARIES
There is no single schema form. Several
schema vocabularies have been developed
in the XML language.
Support for a particular schema depends on
the XML parser being used for validation.

136

SCHEMA VOCABULARIES

XP

This figure shows a few schema vocabularies

137

STARTING A SCHEMA FILE

XP

A schema is always placed in a separate XML


document that is referenced by the instance
document.

138

XP
ELEMENTS AND ATTRIBUTES OF
THE PATIENTS DOCUMENT
This figure shows the elements and attributes of the patients.xml document

139

SCHEMA TYPES

XP

XML Schema recognize two categories of


element types: complex and simple.
A complex type element has one or more
attributes, or is the parent to one or more
child elements.
A simple type element contains only
character data and has no attributes.
140

SCHEMA TYPES

XP

This figure shows types of elements

141

XP

SIMPLE TYPE ELEMENTS


Use the following syntax to declare a simple type
element in XML Schema:

<element name=name type =type/>


Here, name is the name of the element in the instance
document and type is the data type of the element.
If a namespace prefix is used with the XML Schema
namespace, any XML Schema tags must be qualified
with the namespace prefix.
142

XP

UNDERSTANDING DATA TYPES


XML Schema supports two data types: built-in and
user-derived.
A built-in data type is part of the XML Schema
specifications and is available to all XML Schema
authors.
A user-derived data type is created by the XML
Schema author for specific data values in the
instance document.
143

DECLARING AN ATTRIBUTE

XP

An attribute is another example of a simple type.


The syntax to define an attribute is
<xs:attribute name="name" type="type
default="default fixed="fixed" />
Where name is the name of the attribute, type is
the data type, default is the attributes default
value, and fixed is a fixed value for the attribute.

144

ASSOCIATING ATTRIBUTES
AND ELEMENTS

XP

The basic structure for defining a complex type


element with XML Schema is
<xs:element name="name">
<xs:complexType>
declarations
</xs:complexType>
</xs:element>
Where name is the name of the element and
declarations is schema commands specific to the
type of complex element being defined.
145

ASSOCIATING ATTRIBUTES
AND ELEMENTS

XP

Four complex type elements that usually appear in an


instance document are the following:
The element is an empty element and contains
only attributes.
The element contains textual content and
attributes but no child elements.
The element contains child elements but not
attributes.
The element contains both child elements and
attributes.
146

EMPTY ELEMENTS
AND ATTRIBUTES

XP

The code to declare the attributes of an empty


element is
<xs:element name="name">
<xs:complexType>
attributes
</xs:complexType>
</xs:element>
Where attributes is the set of declarations that
define the attributes associated with the element.
For example, the empty element
147

XP

SIMPLE CONTENT
AND ATTRIBUTES
If an element is not empty and contains textual content (but no
child elements), the structure of the complex type element is
slightly different.
<xs:element name="name">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="type">
attributes
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

148

XP

SPECIFYING THE USE OF


AN ATTRIBUTE
An attribute may or may not be required with a
particular element. To indicate whether an attribute is
required, you add the use attribute to the element
declaration or reference. The use attribute has the
following values:
requiredThe attribute must always appear with
the element
optionalThe use of the attribute is optional with
the element
prohibitedThe attribute cannot be used with the
element

149

REFERENCING AN ELEMENT XP
OR ATTRIBUTE
XML Schema allows for a great deal of flexibility in
designing complex types. Rather than nesting the
attribute declaration within the element, you can
create a reference to it. The code to create a
reference to an element or attribute declaration is
<xs:element ref="elemName" />
<xs:attribute ref="attName" />
Where elemName is the name used in an element
declaration and attName is the name used in an
attribute declaration
150

XP

WORKING WITH
CHILD ELEMENTS
Another kind of complex type element contains child
elements, but no attributes. To define these child elements,
use the code structure
<xs:element name="name">
<xs:complexType>
<xs:compositor>
elements
</xs:compositor>
</xs:complexType>
</xs:element>
Where elements is the list of simple type element
declarations for each child element, and compositor defines
how the child elements are organized.

151

USING COMPOSITORS

XP

XML Schema supports the following compositors:


sequence defines a specific order for the child
elements
choice allows any one of the child elements to
appear in the instance document
all allows any of the child elements to appear
in any order in the instance document;
however, they must appear either only once or
not all.
152

WORKING WITH CHILD XP


ELEMENTS AND ATTRIBUTES
The code for a complex type element that contains
both attributes and child elements is
<xs:element name="name">
<xs:complexType>
<xs:compositor>
elements
</xs:compositor>
</xs:complexType>
attributes
</xs:element>
153

XP

SPECIFYING MIXED CONTENT

When the mixed attribute is set to the value true, XML Schema assumes that the
element contains both text and child elements. The structure of the child elements
can then be defined with the conventional method. For example, the XML content
<Summary>
Patient <Name>Cynthia Davis</Name> was enrolled in
the <Study>Tamoxifen Study</Study> on 8/15/2003.
</Summary>
can be declared in the schema file using the following complex type:
<element name="Summary">
<complexType mixed="true">
<sequence>
<element name="Name" type="string"/>
<element name="Study" type="string"/>
</sequence>
</complexType>
</element>
154

XP

APPLYING A SCHEMA
To attach a schema to the document, you must do
the following:
Declare a namespace for XML Schema in the
instance document.
Indicate the location of the schema file.
To declare the XML Schema namespace in the
instance document, you add the following attribute to
the documents root element:
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"

155

APPLYING A SCHEMA

XP

If there is no namespace for the contents of


the instance document, add the following
attribute to the root element:
xsi:noNamespaceSchemaLocation="schema"

156

XP

UNDERSTANDING DATA TYPES


A primitive data type, also called a base
type, is one of 19 fundamental data types not
defined in terms of other types.
A derived data type is a collection of 25 data
types that the XML Schema developers
created based on the 19 primitive types.

157

XP

UNDERSTANDING DATA TYPES


This figure shows the 44 built-in data types

158

XP

UNDERSTANDING DATA TYPES


This figure shows a partial description of XML string data types

159

XP

UNDERSTANDING DATA TYPES


This figure shows a partial description of XML numeric data types

160

XP

UNDERSTANDING DATA TYPES


This figure shows a partial description of XML date and time data types

161

XP

DERIVING NEW DATA TYPES


Three components are involved in deriving
new data types:
Value space: the set of values that
correspond to the data type.
Lexical space: the set of textual
representations of the value space.
Facets: the properties of the data type that
distinguish one data type from another.

162

XP

USER DERIVED DATA


New data types fall into three categories:
List: a list of values where each list is
derived from a base type.
Union: the combination of two or more
data types.
Restriction: a limit placed on the facet of a
base type.

163

DERIVING A RESTRICTED
DATA TYPE

XP

The most common way to derive a new data


type is to restrict the properties of a base
type. XML Schema provides twelve
constraining facets for this purpose.

164

CONSTRAINING FACETS

XP

This figure shows the 12 constraining facets

165

XP

The Patterns Facet


A pattern can be created with a formatted text string called a
regular expression or regex.
To apply a regular expression in a data type, you use the
code
<xs:simpleType name="name">
<xs:restriction base="type">
<xs:pattern value="regex"/>
</xs:restriction>
</xs:simpleType>
Where regex is a regular expression pattern.

166

PATTERN QUANTIFIERS

XP

This figure shows pattern quantifiers

167

XP

WORKING WITH NAMED TYPES

Since content can be either simple or complex, it is not surprising that XML
Schema also allows schema authors to create customized complex types.
The advantage of creating a complex type is that the complex structure can
be reused in the document.
For example, the following code declares an element named client
containing the complex content of two child elements named firstName and
lastName:
<xs:element name="client">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
168

XP

NAMED MODEL GROUPS


A named model group is a collection, or group,
of elements. The syntax for creating a model
group is
<xs:group name="name">
elements
</xs:group>
Where name is the name of the model group,
and elements is a collection of element
declarations

169

WORKING WITH NAMED


ATTRIBUTE GROUPS

XP

Attributes can be grouped into collections called


named attribute groups.
This is particularly useful for attributes that you want
to use with several different elements in a schema.
The syntax for a named attribute group is
<xs:attributeGroup name="name">
attributes
</xs:attributeGroup>
Where name is the name of the attribute group and
attributes is a collection of attributes assigned to the
group.
170

XP

STRUCTURING A SCHEMA
One schema design is a Flat Catalog Design.
In this design, all element declarations are made
globally.
The structure of the instance document is
created by referencing the global element
declarations.
The syntax is:

<element ref=name>
171

FLAT CATALOG DESIGN

XP

This figure shows a Flat Catalog design

172

XP

STRUCTURING A SCHEMA
Schemas can be structured in a number of
ways. One structure is called a Russian Doll
design. This design involves sets of nested
declarations.
While this design makes it easy to associate
the schema with the instance document, it
can be confusing and difficult to maintain.

173

RUSSIAN DOLL DESIGN

XP

This figure shows a Russian Doll design

174

VENETIAN BLIND DESIGN

XP

A Venetian blind design is similar to a flat catalog,


except that instead of declaring elements and
attributes globally, it creates named types and
references those types within a single global element
In this layout, the only globally declared element is
the patients element; all other elements and
attributes are placed within element or attribute
groups or, in the case of the performance element,
within a named complex type
175

VENETIAN BLIND DESIGN

XP

176

XP

COMPARING SCHEMA DESIGNS


This figure compares the three schema designs

177

PLACING A SCHEMA IN A XP
NAMESPACE: TARGETING A
NAMESPACE
To associate a schema with a namespace, you first
declare the namespace and then make that
namespace the target of the schema. To do this,
you add the following attributes to the schemas
root element:
prefix:xmlns="uri"
targetNamespace="uri"
Where prefix is the prefix of the XML Schema
namespace and uri is the URI of the target
namespace
178

XP

VALIDATING A COMBINED
DOCUMENT
This figure shows how schemas are combined when the data is combined

179

APPLYING A SCHEMA TO A XP
DOCUMENT WITH A NAMESPACE
To apply a schema to a document with a namespace,
add the following attributes to the instance
documents root element:
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:schemaLocation="uri schema"
Where uri is the URI of the namespace and schema
is the location and name of the schema file.
All global elements and attributes from the schema
must be qualified in the instance document.
180

INCLUDING AND IMPORTINGXP


SCHEMAS
To include a schema from the same namespace, add the
following element as a child of the schema element:
<xs:include schemaLocation="schema" />
Where schema is the name and location of the schema file.
To import a schema from a different namespace, use the
syntax
<xs:import namespace="uri" schemaLocation="schema" />
Where uri is the URI of the imported schemas namespace
and schema is the name and location of the schema file.

181

XP
REFERENCING OBJECTS FROM
OTHER SCHEMAS

Once a schema is imported, any objects it


contains with global scope can be referenced
To reference an object from an imported schema,
you must declare the namespace of the imported
schema in the schema element
You can then reference the object using the ref
attribute or the type attribute for customized
simple and complex types

182

TUTORIAL 5

XP

WORKING WITH XSLT AND XPATH

183

OBJECTIVES

XP

In this chapter, you will:


Learn about the history and theory of XSL

Understand XPath and examine a node tree


Create an XSLT style sheet
Be introduced to syntax of the XPath language
Transform an XML document into an HTML file
Create templates to format sections of the XML
document
184

XP

OBJECTIVES
In this chapter, you will:
Sort the contents of an XML document
Create conditional nodes to generate different HTML
code
Use predicates to select subsets of an XML
document
Insert new elements and attributes in the transformed
document

185

THE HISTORY OF XSL

XP

In 1998, the W3C developed the Extensible Style


sheet Language, or XSL
XSL is composed of three parts:
XSL-FO (Extensible Style sheet Language
Formatting Objects)
XSLT (Extensible Style sheet Language
Transformations)

186

INTRODUCING XSLT STYLE XP


SHEETS AND PROCESSORS
An XSLT style sheet contains instructions for
transforming the contents of an XML document into
another format
An XSLT style sheet document is itself an XML
document
An XSLT style sheet document has an extension .xsl

187

XP

GENERATING A RESULT
DOCUMENT
An XSLT style sheet converts a source document of
XML content into a result document by using the XSLT
processor

188

INTRODUCING XSLT STYLE XP


SHEETS AND PROCESSORS
The transformation can be performed by a server or a
client
In a server-side transformation, the server receives a
request from a client, applies the style sheet to the
source document, and returns the result document to the
client
In a client-side transformation, a client requests retrieval
of both the source document and the style sheet from the
server, then performs the transformation, and generates
the result document
189

XP

CREATING AN XSLT STYLE SHEET


To create an XSLT style sheet, the general structure:
<?xml version =1.0>
<xsl:stylesheet version = 1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
Content of the style sheet
</xsl:stylesheet>
The <xsl:stylesheet> tag can be substituted for the
<xsl:transform> tag
190

WORKING WITH DOCUMENT


NODES

XP

Under XPath, each component in the document is


referred to as a node, and the entire structure of the
document is a node tree
The node tree consists of the following objects:
the source document itself
comments
processing instructions
namespaces
elements,
element text
element attributes
191

NODE TREE EXAMPLE

XP

192

WORKING WITH DOCUMENT


NODES

XP

At the top of the node is the root node


A node that contains other nodes is called a
parent node, and the nodes contained in the
parent are called child nodes
Nodes that share a common parent are
called sibling nodes
Any node below another node is referred to
as a descendant of that node
193

WORKING WITH DOCUMENT


NODES

XP

Nodes are distinguished based on the


object they refer to in the document
A node for an element is called an element
node
The node that stores element attributes is
called an attribute node

194

XP
USING XPATH TO REFERENCE A
NODE
XPath provides the syntax to refer to the various nodes in
the node tree
The syntax is used by operation system to specify file
pathnames
The location of a node can be expressed in either
absolute or relative terms
XPath also does data extraction

195

RELATIVE PATHS

XP

With a relative path, the location of the node is


indicated relative to a specific node in the tree called
the context node

196

USING XPATH TO REFERENCEXP


A NODE
For absolute path, XPath begins with the root node,
identified by a forward slash and proceeds down the
levels of the node tree
An absolute path: /child1/child2/child3/
To reference an element without regard to its location
in the node tree, use a double forward slash with the
name of the descendant node
A relative path : //descendant

197

REFERENCING GROUPS OF
ELEMENTS

XP

XPath allows you to refer to groups of nodes by using the


wildcard character (*)
To select all of the nodes in the node tree, you can use the
path:
//*
The (*) symbol matches any node, and the (//)symbol
matches any level of the node tree
Example: /portfolio/stock/*

198

REFERENCING ATTRIBUTE
NODES

XP

XPath uses different notation to refer to attribute


nodes
The syntax for attribute node is:
@attribute
where attribute is the name of the attribute
Example: /portfolio/stock/name/@symbol

199

XP

WORKING WITH TEXT NODES


The text contained in an element node is treated as a
text node
The syntax for referencing a text node is:
text()
To match all text nodes in the document, use:
//text()

200

XP

CREATING THE ROOT TEMPLATE


A template is a collection of elements that define how
a particular section of the source document should be
transformed in the result document
The root template sets up the initial code for the result
document

201

CREATING A TEMPLATE

XP

To create a template, the syntax is:


<xsl:template match=node set>
styles

</xsl:template>
where node set is an XPath expression that
references a node set from the source document
and styles are the XSLT styles applied to those
nodes

202

CREATING A ROOT TEMPLATE

XP

To create a root template, the syntax is:


<xsl:template match=/>
styles

</xsl:template>

203

XP

CREATING THE ROOT TEMPLATE

A template contains two types of content: XSLT


elements and literal result elements
XSLT elements are those elements that are part of
the XSLT namespace and are used to send
commands to the XSLT processor
A literal result element is text sent to the result
document, but not acted upon by the XSLT
processor
204

XP
CREATING THE ROOT TEMPLATE
EXAMPLE

205

XP

SPECIFYING THE OUTPUT METHOD


By default, the XSLT processor will render the result
document as an XML file
To control how the processor formats the source
document, you can specify the output method using the
<xsl:output /> element

206

ATTRIBUTS OF THE
<XSL:OUTPUT/> ELEMENT

XP

207

XP

TRANSFORMING A DOCUMENT
A browser with a built-in XSLT processor allows you to
view the result document
Alternatively, you can use XML Spy to create the result
document as a separate file, and then view that file in
your browser
Most XSLT processors provide the capability to create
the result document as a separate file

208

VIEWING THE RESULT


DOCUMENT IN A BROWSER

XP

Internet Explorer 6.0 contains built-in XSLT processor


You can view the results of the transformation by
opening the result document in the browser

209

XP
CREATING AN HTML FILE IN XML
SPY
One advantage of creating a separate HTML file is that it
can be viewed in any Web browser
You have to regenerate the HTML file every time you
make a change to the source document, or the style
sheet
The XSLT processor adds one extra line to the
document that provides additional information to the
browser about the content of the document and its
encoding

210

XP
TRANSFORMING THE SOURCE
DOCUMENT IN XML SPY

211

XP

EXTRACTING ELEMENT VALUES


To insert a nodes value into the result document, the
syntax is:
<xsl:value-of> select=expression />
where expression is an expression that identifies the
node from the source documents node tree
If the node contains child elements in addition to text
content, the text in those child nodes appears as well

212

INSERTING A NODE VALUE


EXAMPLE

XP

213

PROCESSING SEVERAL
ELEMENTS

XP

To process a batch of nodes, the syntax is:


<xsl:for-each select=expression />
styles
</xsl:for-each>
where expression is an expression that defines
the group of nodes to which the XSLT and
literal result elements are applied
214

PROCESSING SEVERAL
ELEMENTS

XP

215

WORKING WITH TEMPLATES

XP

To apply a template in the result document, use the


XSLT element
<xsl:apply-templates select=expression />
where expression indicates the node template to
be applied

216

XP

CREATING TEMPLATE EXAMPLE

217

XP

USING THE BUILT-IN TEMPLATES


Each node has its own built-in template.
The built-in template for element nodes matches the
document root and all elements in the node tree
The built-in template for text nodes matches all text
nodes and causes their values to appear in the result
document
For example, you can add the stock template to the
style sheet

218

CREATING THE STOCK


TEMPLATE EXAMPLE

XP

219

SORTING NODE SETS

XP

By default, nodes are processed in document order,


by their appearance in the document
To specify a different order, XSLT provides the
<xsl:sort> element
This element can be used with either the <xsl:applytemplates> or the <xsl:for-each> element

220

SORTING NODE SETS

XP

The <xsl:sort> element contains several attributes to


control how the XSLT process sorts the nodes in the
source document
The select attribute determines the criteria under
which the context node is sorted
The data-type attribute indicates the type of data
The order attribute indicates the direction of the
sorting (ascending or descending)

221

XP

CREATING CONDITIONAL NODES


XSLT supports two kinds of conditional elements:
<xsl:if>
<xsl:choose>
To apply a format only if a particular condition is met ,
use the <xsl:if> element
To test for multiple conditions and display different
outcomes, use the <xsl:choose> element

222

XP
CREATING CONDITIONAL NODES
EXAMPLE

223

XP
USING COMPARISON
OPERATORS AND FUNCTIONS

224

XP

WORKING WITH PREDICATES


Predicates are XPath expressions that test for a
condition and create subsets of nodes that fulfill that
condition
The predicate can also indicate the position of the
node in the node tree
To select a specific position in the source document,
use the position() function combined with any XPath
expression

225

ADDING PREDICATES TO THEXP


ROOT TEMPLATE EXAMPLE

226

CREATING ELEMENTS AND XP


ATTRIBUTES
To create an element, XSLT uses the <xsl:element>
tag
The namespace attribute assigns a name to the
element
The namespace attribute provides a namespace
The use-attribute provides a list of attribute-sets

227

CREATING AN ELEMENT

XP

To create the <a> element in the result


document, use the <xsl:element> tag

228

CREATING AN ATTRIBUTE

XP

Attributes are created in XSLT by using the <xsl:attribute>


element
The name attribute specifies the name of the attribute
The namespace attribute indicates the namespace
You can create inline images in the result document by
using the attribute tag

229

CREATING AN ATTRIBUTE

XP

To add the href attribute to the <a> tag, use the


<xsl:attribute> element

230

CREATING COMMENTS AND XP


PROCESSING INSTRUCTIONS
The <xsl:comment> element creates the comment
You can create a processing instruction by using the
<xsl:processing-instruction> element
If you want to add a processing instruction to attach
the result document to the style.css sheet, use the
following code:

231

SUMMARY

XP

Extensible Style sheet Language,or XSL, is composed of


three parts: XSL-FO, XSLT, and XPath
XPath language is used to reference a node
Templates are used to format sections of the XML
document and transform XML data into a variety of
formats

232

XP

SUMMARY
Nodes can be sorted in either alphabetical or numerical
order
Comparison elements allow changing the contents of the
result document based on the values of the nodes in the
source document
Predicates are used to create subsets of the source
documents node tree
You can insert new elements and attributes in the
transformed document

233

You might also like