Professional Documents
Culture Documents
XP
INTRODUCING XML
XP
INTRODUCING XML
XP
XP
XP
XP
THE 10 PRIMARY XML DESIGN
GOALS
1.
2.
3.
4.
XP
THE 10 PRIMARY XML DESIGN
GOALS CONTINUED
XML VOCABULARIES
XP
XP
WELL-FORMED AND VALID XML
DOCUMENTS
11
12
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
16
ELEMENT
XP
XP
ELEMENTS
Nested elements are called child elements.
Elements must be nested correctly. Child elements
must be enclosed within their parent elements.
18
XP
19
XP
20
document
elements
{
21
CHARACTER REFERENCES
XP
22
CHARACTER REFERENCES
XP
23
CHARACTER REFERENCES
XP
24
CHARACTER REFERENCES
XP
character
reference
25
XP
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
CDATA SECTIONS
XP
CDATA section
29
XP
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
32
XP
DISPLAYING AN XML DOCUMENT
IN A WEB BROWSER
33
XP
XP
APPLYING A STYLE TO AN
ELEMENT
XP
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
XP
This figure shows the cascading style sheet stored in the jw.css file
40
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
42
TUTORIAL 2
XP
43
COMBINING XML
VOCABULARIES
XP
44
XP
NAME COLLISION
XP
46
DECLARING A NAMESPACE
XP
DECLARING A NAMESPACE
XP
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
50
TUTORIAL 3
XP
51
XP
52
XP
53
CUSTOMER INFORMATION
COLLECTED BY KRISTEN
XP
54
XP
THE STRUCTURE OF KRISTENS
DOCUMENT
This figure shows the overall structure of Kristens document
55
DECLARING A DTD
XP
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
DECLARING A DTD
XP
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
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
63
DECLARING A DTD
XP
DECLARING A DTD
XP
65
DECLARING A DTD
XP
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
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
XP
XP
75
XP
76
MODIFYING SYMBOLS
XP
MODIFYING SYMBOLS
XP
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
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
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
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.
WORKING WITH
ATTRIBUTE TYPES
XP
WORKING WITH
ATTRIBUTE TYPES
XP
WORKING WITH
ATTRIBUTE TYPES
XP
ATTRIBUTE TYPES
XP
94
ATTRIBUTE DEFAULTS
XP
INSERTING ATTRIBUTE-LIST XP
DECLARATIONS
This figure the revised contents of the Orders.xml file
attribute declaration
96
XP
XP
98
XP
XP
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
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
XP
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
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
XP
DECLARING A NAMESPACE AS
AN ELEMENT ATTRIBUTE
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
116
117
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
COMBINING STANDARD
VOCABULARIES
XP
122
XP
XP
124
XP
ADDING THE ELEMENTS OF THE
PARTS VOCABULARY
XP
ADDING THE ELEMENTS OF THE
PARTS VOCABULARY
126
XP
XP
128
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
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
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
137
XP
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
SCHEMA TYPES
XP
141
XP
XP
DECLARING AN ATTRIBUTE
XP
144
ASSOCIATING ATTRIBUTES
AND ELEMENTS
XP
ASSOCIATING ATTRIBUTES
AND ELEMENTS
XP
EMPTY ELEMENTS
AND ATTRIBUTES
XP
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
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
XP
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
156
XP
157
XP
158
XP
159
XP
160
XP
161
XP
162
XP
163
DERIVING A RESTRICTED
DATA TYPE
XP
164
CONSTRAINING FACETS
XP
165
XP
166
PATTERN QUANTIFIERS
XP
167
XP
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
169
XP
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
XP
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
XP
174
XP
XP
176
XP
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
181
XP
REFERENCING OBJECTS FROM
OTHER SCHEMAS
182
TUTORIAL 5
XP
183
OBJECTIVES
XP
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
XP
186
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
XP
XP
XP
192
XP
XP
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
196
197
REFERENCING GROUPS OF
ELEMENTS
XP
198
REFERENCING ATTRIBUTE
NODES
XP
199
XP
200
XP
201
CREATING A TEMPLATE
XP
</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
XP
</xsl:template>
203
XP
XP
CREATING THE ROOT TEMPLATE
EXAMPLE
205
XP
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
XP
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
212
XP
213
PROCESSING SEVERAL
ELEMENTS
XP
PROCESSING SEVERAL
ELEMENTS
XP
215
XP
216
XP
217
XP
218
XP
219
XP
220
XP
221
XP
222
XP
CREATING CONDITIONAL NODES
EXAMPLE
223
XP
USING COMPARISON
OPERATORS AND FUNCTIONS
224
XP
225
226
227
CREATING AN ELEMENT
XP
228
CREATING AN ATTRIBUTE
XP
229
CREATING AN ATTRIBUTE
XP
230
231
SUMMARY
XP
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