final xml ppt - bk
TRANSCRIPT
-
8/8/2019 Final XML Ppt - Bk
1/231
-
8/8/2019 Final XML Ppt - Bk
2/231
Difference between SGML, HTML, and XML
A markup language uses tags to indicate how a file should look whendisplayed on a Web page.
Some examples of markup languages are Standard Generalized markuplanguage (SGML) and Hypertext Markup Language (HTML).
SGML allows documents to describe their own grammar by specifying thetag set used in the document and the structural relationship that these tags
represent. SGML was complex and difficult for developers to master. HTML is based
on SGML.
In HTML, a user has to work with a set of predefined tags that isunderstood by the browser.
XML is a subset of SGML. HTML is used for data presentation, whereas XML is used for datadescription and definition. Thus, the purpose of these two markuplanguages is completely different.
-
8/8/2019 Final XML Ppt - Bk
3/231
Advantages of XML
The advantages provided by XML are as follows:
Domain Specific Vocabulary
Data Interchange
Smart Searches Granular Updates
User-Selected View of Data
-
8/8/2019 Final XML Ppt - Bk
4/231
XML provides a way to store structured data that is capable of beingrecognized by different kinds of devices. In other words, it enables device-
independence. Before you store data in an XML document, you need to organize it.
An XML document is composed of a number of components that can beused for representing information. These components are:
Processing Instruction
An XML document usually begins with the XML declarationstatement or the Processing Instruction (PI).
The PI provides information regarding the way in which the XML fileshould be processed.
The PI statement can be written as:
In the above example, the PI states that version 1.0 is used.
The PI uses the encoding property to specify information about theencoding scheme that is used to create the XML file.
-
8/8/2019 Final XML Ppt - Bk
5/231
Tag
Tags are used to specify a name for a given piece of information.
Tags usually occur in pairs.
Each pair consists of a start tag and an end tag.
The start tag only contains the name of the tag while the end tagincludes a forward slash (/) before the name of the tag.
Elements
Elements are the basic units that are used to identify and describedata in XML.
They are the building blocks of an XML document.
Elements are represented using tags.
An XML document must always have a root element. All other
elements are specified within the opening and closing tags of theroot element.
Content
The information that is represented by the elements of an XMLdocument is referred to as the content of that element.
-
8/8/2019 Final XML Ppt - Bk
6/231
-
8/8/2019 Final XML Ppt - Bk
7/231
Attributes
Attributes provide additional information about the elements for
which they are declared. An attribute consists of a name-value pair.
Elements can have one or more attributes. Attributes orattribute values can be either mandatory or optional.
While deciding whether to represent information as an element
or an attribute, you can follow the guidelines given below: If the data must be displayed, you can represent it as an
element. In general, element attributes are used forintangible, abstract properties such as ID.
If the data must be updated frequently, it is better
represented as an element because it is easier to editelements than attributes with XML editing tools. If thevalue of a piece of information must be checkedfrequently, it may be represented as an attribute.
-
8/8/2019 Final XML Ppt - Bk
8/231
Entities
An entity can be described as a short cut to a set of information. It is a name that is associated with a block of data. This data can be a chunk of text or a reference to an external file that
contains textual or binary information. XML supports the use of three kinds of entities: internal, general, and
parameter entities. Internal Entities An internal entity consists of a name that is associated with a block of
information. This can be identified easily as it is always preceded by an ampersand(&) symbol and terminated with a semicolon.
Comments Comments are statements that are used to explain the code. When the code is executed, comment entries are ignored by the parser. Comments are not essential in an XML file.
-
8/8/2019 Final XML Ppt - Bk
9/231
Rules for Creating Well-formed XML Documents
Every start tag must have an end tag.
Empty tags must be closed using a forward slash (/). All attribute values must be given in double quotation marks.
Tags must nest correctly.
XML tags are case-sensitive. They must match each other in everyimplementation.
-
8/8/2019 Final XML Ppt - Bk
10/231
Document Type Definition
A DTD defines the structure of the content of an XML document, thereby allowing you
to store data in a consistent format. XML allows you to create your own DTDs for applications.
You can check an XML document against a DTD. This checking process is called validation. XML documents that conform to a DTD are considered valid documents.
As a DTD allows you to specify the structure and type of data elements, a DTD canbe created to specify the structure of the document.
In DTD, elements are declared by using the following syntax:
Elements can be of following types: Empty Unrestricted Container
While declaring elements in a DTD, different symbols can be used to specify whetheran element is mandatory or optional, and whether it can occur more than once.
-
8/8/2019 Final XML Ppt - Bk
11/231
The following table lists the various symbols that can be used while definingthe DTD:
Symbol Meaning Example Description
, and in specific
order
PRODUCTNAME,
DESCRIPTION
PRODUCTNAMEor
DESCRIPTIONmust occur
in that order.
| or PRODU CTNAME|DESC
RIPTION
Either PRODUCTNAMEor
DESCRIPTION.
? optional , can
occur onlyonce.
DESCRIPTION? DESCRIPTIONneed not be
present, but if it is present, it
can occur onlyonce.
* An element can
occur zero or
multiple times.
(PRODUCTNAME|DES
CRIPTION)*
Anynumber of
PRODUCTNAMEor
DESCRIPTIONelements
can be present in anyorder.
+ An element must
occuratleast
once. There can
be multiple
occurrences.
DESCRIPTION+ DESCRIPTIONcan occur
multiple times.
-
8/8/2019 Final XML Ppt - Bk
12/231
The syntax for declaring attributes in a DTD is as follows:
The attributename valuetype [attributetype][default] section is repeated as often as necessary to create
multiple attributes for any given element.
The value types that can be specified for attributes in a DTD are:
PCDATA
ID
(enumerated)
The attribute types are:
REQUIRED
FIXED
IMPLIED
-
8/8/2019 Final XML Ppt - Bk
13/231
to validate thestructure of data.
To validate the structure of data in an XML document you need to useparsers.
Parsers are software programs that check the syntax used in an XML file.There are two types of parsers. They are:
Non-validating parsers: Check whether an XML document is well-formed.
Validating parsers: Check for well-formedness and validity of an XMLdocument.
-
8/8/2019 Final XML Ppt - Bk
14/231
-
8/8/2019 Final XML Ppt - Bk
15/231
To ensure that the structure of an XML document conforms to the DTD,you must associate the DTD with the XML document.
The declaration is used to define the internal DTD. It can
also be used to reference an external DTD.
The syntax for defining an internal DTD in an XML document is as follows:
The syntax for referencing an external DTD in the XML document is asfollows:
-
8/8/2019 Final XML Ppt - Bk
16/231
Introduction to XML Schemas
AnXML schemais used to define the structure of an XML document.
Microsoft has developed a language that is used to define the schema ofan XML document. This language is called the XML Schema Definition(XSD) language.
Advantages of XML Schemas overDTDs
Some of the advantages of an XML schema created by using XSD over
DTD are as follows: XSD provides more control over the type of data that can be assignedto elements and attributes as compared to DTD.
DTD does not enable you to define your own customized data types.XSD enables you to create your own data types.
XSD also allows you to specify restrictions on data.
The syntax for defining a DTD is different from the syntax used for creating an XML document. However, the syntax for defining an XSD isthe same as the syntax of the XML document.
-
8/8/2019 Final XML Ppt - Bk
17/231
-
8/8/2019 Final XML Ppt - Bk
18/231
declaring a simple type element.
A simple element does not contain any child elements or attributes. Simpleelements contain only values such as numbers, strings, and dates.
The syntax for declaring elements with a simple type is as follows:
You can associate an element with a user-defined simple type. To do so,you must define the new simple type.
You can use the simpleType element of XSD to create a user-defined simpletype.
-
8/8/2019 Final XML Ppt - Bk
19/231
declaring a complex type element. A complex type element is one that contains other markup elements, attributes, and
mixed content. To declare a complex type element, you need to first define a complex type. After you
define a complex type, you can declare a complex element by associating this datatype with the element. You can define a complex type by using the syntax given below:
Content model declaration
To declare an element as a complex type element, the element must be associated
with a complex type. For example, to declare the element PRODUCT as a complex type element you canassociate this element with the prdt data type as shown below:
You can create complex type elements by associating them with complex types.
You can use the element element of XSD to declare a complex type element.
You can use the complexType element of XSD to create the complex type.
-
8/8/2019 Final XML Ppt - Bk
20/231
-
8/8/2019 Final XML Ppt - Bk
21/231
declaring an attribute in an XML schema.
The attribute Element
In XSD, an attribute for a user-defined element is declared using theattribute element.
The syntax for declaring an attribute in XSD is given below:
The attribute element contains attributes that are used to furtherqualify and restrict the scope and usage of the user-defined attribute.These attributes are discussed below.
The nameAttribute
Is used to specify the name of a user-defined attribute.
Must be used when the schema element is the parent element ofthe attribute element.
-
8/8/2019 Final XML Ppt - Bk
22/231
-
8/8/2019 Final XML Ppt - Bk
23/231
Global Attributes
Globalattributes are attributes that are declared outside all element
declarations. For such attributes, the schema element is the parentelement.
After declaring a global attribute, you can reuse this attribute fromanywhere within the schema.
Global attributes can be associated with both simple and complex datatypes.
-
8/8/2019 Final XML Ppt - Bk
24/231
-
8/8/2019 Final XML Ppt - Bk
25/231
XML Namespaces
Introduction to XML Namespaces
A namespace is a method of preventing conflicts between elementshaving same names.
In XML, a namespace is a virtual space that is assigned or recognizedby a Uniform Resource Identifier (URI).
A URI is a string that uniquely identifies the elements and attributesfrom different schemas.
A namespace URI is not the same as a Web URI. It does not actuallypoint to a resource on the Internet. It is just a unique identifier used toresolve conflicts between elements having same names.
You can assign any name or string as a URI.
XML allows you to map a prefix to a URI. You can think of a prefix as analias for a namespace URI.
-
8/8/2019 Final XML Ppt - Bk
26/231
Declaring Namespaces
A namespace can be declared in the XSD document by using the xmlnskeyword.
This keyword is an attribute of the schema element, which is declared at thebeginning of the document.
The general form of the xmlns keyword is as follows:
xmlns:prefix=URI
There are two kinds of namespace declarations, default and explicit.
Default Declaration
The default declaration statement enables you to declare a default namespacefor a document.
In case of a default namespace, you need not specify the prefix.
Explicit Declaration
In an explicit declaration, the xmlns keyword associates a prefix with anamespace URI, as shown in the following example:
:
Only those elements and attributes that explicitly use this prefix come under thescope of the specified namespace.
-
8/8/2019 Final XML Ppt - Bk
27/231
mechanism for applying restrictions across multiple documents.
The components declared in one schema can be reused by anotherschema.
This can be done either by using the include orimport elements.
The include element The syntax for using the include element is given below:
The include element can have multiple occurrences in an XSD
document.
The schema element is the parent element of the include element.
The include element allows reference to any external schema, which
is defined in the context of the same target namespace.
A target namespace has a reference to a URI to which a schemabelongs.
The target namespace for a schema is declared using the
targetNamespace attribute of the schema element.
-
8/8/2019 Final XML Ppt - Bk
28/231
The import element
The import element performs the same function as the includeelement. However, the import element allows you to access
components from multiple schemas that may belong to different targetnamespaces.
The syntax for using the import element is given below:
The id attribute takes the ID of the element as its value. This ID mustbe unique for the XSD document. This attribute is optional.
The namespace attribute is used to specify a URI. It also specifies the
prefix, which is used to associate an element or an attribute with aparticular namespace.
The schemaLocation attribute is identical to the one used by the
include element. The value of this attribute is set to the physicallocation of the specified schema file.
-
8/8/2019 Final XML Ppt - Bk
29/231
XSD enables you to combine related elements and attributes into groups.This feature of creating grouped elements and attributes enables you toperform the following tasks:
Create a reusable group of elements and attributes:
A reusable group can be used to specify the content model for acomplex type.
This eliminates the task of declaring the elements, which havealready been declared in some other context.
Select a single element from a group:
At times, you may want to use just one element or attribute from agroup. For example, you may want to specify either the residenceor the office phone number of an employee.
You can create a group of such elements and allow one of them tobe used in the XML document.
Specify the sequence of elements:
You can create a group of elements and specify the sequence inwhich each element in the group should appear in the XMLdocument.
-
8/8/2019 Final XML Ppt - Bk
30/231
XSD provides a number of elements that can be used to group user-defined elements and attributes. These are:
sequence
group
choice
all
attributeGroup
The sequence Element
The sequence element helps you ensure that elements declared
within the opening and closing tags of this element appear in a specificorder.
The group Element
In XSD, a set of elements can be grouped together by a commonname and incorporated into a complex data type.
Grouping of elements is beneficial when you want a set of relatedelements to be referred by a common name.
-
8/8/2019 Final XML Ppt - Bk
31/231
The following is the syntax for declaring a group element:
The following table describes the attributes of the group element:
Attribute Description
maxOccurs Used to specify the maximum number of times a groupcan occur in the XML document. The value of the
maxOccurs attribute must be an integer greater than orequal to zero. If you do not want to set a limit on themaximum number, you can specify "unbounded" as thevalue to this attribute.
minOccurs Used to specify the minimum number of times a groupcan occur in the XML document. The value of the
minOccurs attribute must be an integer greater than orequal to zero. To specify that the group is optional, setthe value of this attribute to zero.
name Used to assign a name for the group element. The nameassigned to the group must not contain any colon.
ref Used to refer to a group in a complex type element.
-
8/8/2019 Final XML Ppt - Bk
32/231
The choice Element
XSD enables you to specify alternative options by using the choiceelement.
The choice element allows only one of the elements contained in thegroup to be present within the parent element.
The syntax for declaring a choice element:
In the above syntax, id, maxOccurs, and minOccurs are the attributes
of the choice element. These attributes are similar to the attributes of the group element
mentioned earlier.
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 thechild elements must appear.
The syntax for using the all element:
In the above syntax, the maxOccurs and minOccurs attributes havethe same meaning as the maxOccurs and minOccurs elements of the
group element.
-
8/8/2019 Final XML Ppt - Bk
33/231
The attributeGroup Element XSD also enables you to group attributes that can be reused with
different elements.
You can group attributes by using the attributeGroup element. The syntax of the attributeGroup element:
attribute1
attribute2
:
In the above syntax, attribute1 and attribute2 are the declaration
statements of the various attributes that are to be grouped using theattributeGroup element.
-
8/8/2019 Final XML Ppt - Bk
34/231
Cascading Style Sheets (CSS)
A CascadingStyle 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.
The CSS file can be included in a number of XML documents that havethe same data structure.
-
8/8/2019 Final XML Ppt - Bk
35/231
Task 3:Create a style sheet to format
data.
CSS allows you to format the contents of adocument by specifying the element namefollowed by the formatting instructions forthat element. The syntax for coding a CSS
is as follows:elementname
{
property1: value;
-
8/8/2019 Final XML Ppt - Bk
36/231
Task 4:Apply the style sheet to the XML
document.
In order to apply the formattingspecifications given in the CSS file to thedata in an XML document, you need toassociate the CSS with the XML
document. This can be done using the following
syntax:
-
8/8/2019 Final XML Ppt - Bk
37/231
-
8/8/2019 Final XML Ppt - Bk
38/231
-
8/8/2019 Final XML Ppt - Bk
39/231
Task 2:Identify a mechanism
for(Contd.)
A software calledXSLTprocessorreadsthe instructions given in XSLT andtransforms the XML document into anotherXML document, which uses formatting
objects of XSL to display the XMLdocument in the desired format.
-
8/8/2019 Final XML Ppt - Bk
40/231
Task 2:Identify a mechanism
for(Contd.)
Comparison between XSLT and
CSS
XSLT is a superset of the CSSfunctionality.
XSLT is an application of XML. Therefore,it follows the XML syntax, whereas CSShas its own syntax. XML and CSS can co-exist since they meet different needs.
XSLT is intended for com lex formattin .
-
8/8/2019 Final XML Ppt - Bk
41/231
Task 2:Identify a mechanism
for(Contd.)
Working of the XSLT ProcessorXSLT style sheet
XML document
Result tree
MSXML Parser
XSLT tree
Source tree
XSLT
processor
-
8/8/2019 Final XML Ppt - Bk
42/231
Task 2:Identify a mechanism
for(Contd.)
Result Since XSL gives you added functionality of
extracting specific data from an XMLdocument, XSL can be used to select dataas per the scenario.
You need to use XSLT, which is a part ofXSL, to create a style sheet. This style
sheet should contain the instructions for
-
8/8/2019 Final XML Ppt - Bk
43/231
Task 3:Identify the elements required to
display
selective data in a sorted order.
XSLT provides a number of elements forselecting and formatting data.
Some of the XSLT elements are:
stylesheet
value-of
for-each
sort
-
8/8/2019 Final XML Ppt - Bk
44/231
-
8/8/2019 Final XML Ppt - Bk
45/231
-
8/8/2019 Final XML Ppt - Bk
46/231
Task 3:Identify thesorted order.
(Contd.)
The for-each element
This element is used to instruct the XSLTprocessor to process the information for eachinstance of the specified pattern.
The syntax for using the for-each elementis as follows:
[action to be performed]
-
8/8/2019 Final XML Ppt - Bk
47/231
Task 3:Identify thesorted order.
(Contd.)
The for-each element (
Contd.) The select attribute of the for-each element
allows you to specify the elements orattributes on which the action needs to be
performed. The XSLT transformation instructions within
the for-each element are applied to each of
the nodes selected by the select attribute of
the for-each element.
-
8/8/2019 Final XML Ppt - Bk
48/231
-
8/8/2019 Final XML Ppt - Bk
49/231
Task 3:Identify thesorted order.
(Contd.)
The sort element (C
ontd.) The syntax for using the sort element is asfollows:
-
8/8/2019 Final XML Ppt - Bk
50/231
-
8/8/2019 Final XML Ppt - Bk
51/231
XSLT Template Rules
XSLT allows you to define template rules
for the output. A template rule describes how an XML
element and its contents are convertedinto a format that can be displayed in thebrowser.
A template rule consists of two parts: A pattern that identifies an XML element in an XML
document.
-
8/8/2019 Final XML Ppt - Bk
52/231
-
8/8/2019 Final XML Ppt - Bk
53/231
XSLT Template Rules (Contd.)
The apply-templates Element
This element is used to instruct the XSLTprocessor to find an appropriate template andperform the specified tasks on each selectedelement.
The syntax for using this element is asfollows:
The select attribute is optional and is used tos ecif the context in which the tem late
-
8/8/2019 Final XML Ppt - Bk
54/231
XSLT Template Rules (Contd.)
The apply-templates Element (Contd.)
You can effectively use templates in order to
display data.
The XSLT processor always starts processingfrom the root node. Therefore, it startsprocessing from the template rule.
If you have more than one template rulecreated for the same element, the previous
template rule will be overridden by the other
-
8/8/2019 Final XML Ppt - Bk
55/231
XSLT Template Rules (Contd.)
-
8/8/2019 Final XML Ppt - Bk
56/231
-
8/8/2019 Final XML Ppt - Bk
57/231
XSLT Template Rules (Contd.)
Default Template (Contd.)
The default template rule consists of the following
statements:
-
8/8/2019 Final XML Ppt - Bk
58/231
Task 3:Identify thesorted order. (Contd.)
Result
The following XSLT elements are used to
extract and format the required data: stylesheet: To specify the style sheetdeclaration.
for-each: To display the product ID, productname, and price for each instance of the
product element. value-of: To display the values of PRODID,
PRODUCTNAME, and PRICE. sort: To sort the elements and attributes
containing product details based on the productID.
-
8/8/2019 Final XML Ppt - Bk
59/231
Task 4:Create a style sheet.
ActionAction
As per the scenario, the product details,such as PRODUCTID, PRODUCTNAME,and PRICE have to be sorted in theascending order of the PRODID attribute.
To do this, type the necessary code assave the file as product1.xsl.
-
8/8/2019 Final XML Ppt - Bk
60/231
-
8/8/2019 Final XML Ppt - Bk
61/231
Task 6:View the XML document.
-
8/8/2019 Final XML Ppt - Bk
62/231
Problem Statement 5.P.1
CyberShoppe wants to display the detailsof all books to its customers. The bookdetails that have to be displayed are bookID, title, and rate of books. The bookdetails must be displayed in the ascending
order of rate and book ID.
-
8/8/2019 Final XML Ppt - Bk
63/231
Summary
In this lesson you learned that:
Rendering is the process of presenting thedata in an XML document in differentformats.
A style sheet is a separate document thatcontains the formatting rules for one orseveral XML documents.
There are two types of style sheets used
with XML documents. The are:
-
8/8/2019 Final XML Ppt - Bk
64/231
Summary (Contd.)
CSS is used to define the style orappearance of an XML document.
XSL is made up of XSL Transformations(XSLT) and XML Path (XPath).
XSLT is a superset of the CSSfunctionality.
XSLT is used to display selective elementsor attributes, sort data on one or more
elements and rocess the data on the
-
8/8/2019 Final XML Ppt - Bk
65/231
Summary (Contd.)
An XSLT style sheet contains a set oftemplates rules. A template rule describeshow an XML element and its contents areconverted into a format that can bedisplayed in the browser.
Each template rule has two parts: apattern and an action.
The reference to a style sheet file is
included in an XML document by using the
-
8/8/2019 Final XML Ppt - Bk
66/231
-
8/8/2019 Final XML Ppt - Bk
67/231
-
8/8/2019 Final XML Ppt - Bk
68/231
-
8/8/2019 Final XML Ppt - Bk
69/231
-
8/8/2019 Final XML Ppt - Bk
70/231
Task 2: Identify the elements required toformat data
based on a condition.
The if Element
The if element provides a simple if-thenconstruct. The syntax ofif element is as
follows:
[actions to be performed if the
-
8/8/2019 Final XML Ppt - Bk
71/231
Task 2: Identify the elements acondition. (Contd.)
The choose Element
The choose element is used to make achoice when there are two or more possiblecourses of action.
It provides a means for testing multipleconditions.
-
8/8/2019 Final XML Ppt - Bk
72/231
Task 2: Identify the elements acondition.(Contd.)
The syntax of the choose element is asfollows:
[action to be taken]
:
:
-
8/8/2019 Final XML Ppt - Bk
73/231
Task 2: Identify the elements a condition.(Contd.)
The following table lists the comparison and
boolean operators that can be used with thexsl:choose and xsl:if elements:
Operator Meaning Example
= Equalto PRI CE[. = 20]
PRODUCTNAME[. = Mini Bus]
!= Not equal to PRI CE[. != 20]
PRODUCTNAME[. != Barbie Doll]
< Less than PRI CE[. < 20]
> Greater than PRI CE[. > 20]
= Greater than or equal to PRI CE[. >= 20]
-
8/8/2019 Final XML Ppt - Bk
74/231
Task 2: Identify the elements acondition. (Contd.)
Operator Meaning Example
and LogicalAND PRI CE[. > 20and . < 30]
or LogicalOR PRI CE[. = 20 or . = 45]
not Negation
operator
PRICE[not(. = 30)]
The operators given in bold in the above list are Microsoft
extensions to the original list of operators recommended by W3C.
-
8/8/2019 Final XML Ppt - Bk
75/231
-
8/8/2019 Final XML Ppt - Bk
76/231
Task 3: Create a style sheet to formatdata based
on a condition.
Task 4: Apply the style sheet to the XML
document.
Task 5: View the XML document.
-
8/8/2019 Final XML Ppt - Bk
77/231
Just a MinuteThe details about the books sold atCyberShoppe need to be displayed. Books
priced higher than $100 are to be displayedin red, those priced higher than $75 are tobe displayed in blue, and the rest are to bedisplayed in green.
-
8/8/2019 Final XML Ppt - Bk
78/231
-
8/8/2019 Final XML Ppt - Bk
79/231
Problem Statement 6.D.2 (Contd.)The structure of the XML document is givenbelow: SUMMARY
PRODUCT
ORDER
SHIPPING
ADDRESS
QUANTITY
-
8/8/2019 Final XML Ppt - Bk
80/231
Task List Identify the data to be displayed.
Identify a mechanism to displaysummarized data.
Identify the XPath expressions requiredfor performing calculations.
Identify the functions required for
performing calculations. Create an XSLT style sheet containing
XPath patterns and functions.
Create the XML document.
-
8/8/2019 Final XML Ppt - Bk
81/231
Task 1: Identify the data to be displayed.Result
As per the given scenario, the details to bedisplayed are as follows:
Product ID Product Name Price per unit Details about orders placed against the product:
Order number Shipping address Total quantity Order value
Total sales amount for the product
-
8/8/2019 Final XML Ppt - Bk
82/231
-
8/8/2019 Final XML Ppt - Bk
83/231
Task 2: Identify a summarized data.(Contd.)
Result
As XPath provides a set of expressions andfunctions that can match specific patterns,retrieve results, and perform additionaloperations such as calculations, XPath can
be used along with XSLT for displaying datain the given scenario.
-
8/8/2019 Final XML Ppt - Bk
84/231
Task 3: Identify the XPath expressionsrequired forperforming calculations.
XPath expressions can be used to retrieve
data based on specific conditions. You canapply constraints by adding a filter clause,which is otherwise referred to as a filterpattern.
Using XPath, you can create expressions thatcan identify the nodes in an XML documentbased on names and values. You can also
create expressions that identify the
-
8/8/2019 Final XML Ppt - Bk
85/231
Operator/
Special
Character
Example Description
/ /PRODUCTD ATA S elects immediate child elements of
PRODUCTDATA. If this operator occurs at the start
of the pattern, it indicates that the child elements
should be selected from the root node.
// // PRODUCTNAME S earches for the specified elementatanynode
level.
. .PRODU CTNAME Indicates the current context.
* * Selects allelements regardless of the element
name.
@ @PRODU CTID Used as a prefix for the attribute PRODUCTID.
Task 3: Identify the performing calculations. (Contd.)
-
8/8/2019 Final XML Ppt - Bk
86/231
Operator/
Special
Character
Example Description
@* @* Selects allattributes, regardless of the name.
: : Separates the namespace prefix from the element
orattribute name.
( ) (PRI CE*QUANTITY) Used to group operations.
[] [@PRODUCTID='
P001'] Applies a filter pattern.
+ num1 + num2 Adds two numbers, returning their sum.
- num1 - num2 Subtracts num2 from num1, returning the
difference.
Task 3: Identify the performing calculations. (Contd.)
-
8/8/2019 Final XML Ppt - Bk
87/231
Operator/
Special
Character
Example Description
* num1 * num2 Multiplies num1 bynum2, returning the product.
div num1 divnum2 Di vides num1 bynum2, returning the quotient.
mod num1 mod num2 Returns the modulusthat is, divides num1 by
num2and returns the remainder.
.. ../PRODUCTNAME S elects the PRODUCTNAMEelement, whichexists within the parent of the current element.
Task 3: Identify the performing calculations. (Contd.)
-
8/8/2019 Final XML Ppt - Bk
88/231
Task 3: Identify the performing calculations. (Contd.)Result
In the given scenario, an XPath expression can be created forcalculating the order value:
The above expression instructs the XSLT processor to pick up thevalue of the PRICE attribute of the parent element and multiply itby the value of the QUANTITY element.
-
8/8/2019 Final XML Ppt - Bk
89/231
Task 4: Identify the functions required forperforming
calculations.
XPath functions XPath provides a various categories of
functions. They are:
string
node set
boolean
number
-
8/8/2019 Final XML Ppt - Bk
90/231
Task 4: Identify the ... performingcalculations.
(Contd.)
String functions The string functions of XPath are used to
perform string operations, such as finding thelength of a string or converting a string from
uppercase to lowercase.
Some of the string functions provided inXPath are:
strin ob ? : Converts the ar ument to a strin
-
8/8/2019 Final XML Ppt - Bk
91/231
Task 4: Identify the performingcalculations.
(Contd.)
String functions (Contd.)
substring(str, num, num?): Returns a portion ofthe string starting from the position specified inthe second argument. Returns the number of
characters specified in the third argument. substring-before(str, str): Returns the portion of
the first string that precedes the second string.
substring-after(str, str): Returns the portion of the
-
8/8/2019 Final XML Ppt - Bk
92/231
Task 4: Identify the performingcalculations.
(Contd.)
Node-Set functions
Node-set functions are used to manipulatenode sets or to return information about nodesets.
Some of the node-set functions provided inXPath are: last(): Returns the number of the last node in the
current node-set. position(): Returns the position of the current
-
8/8/2019 Final XML Ppt - Bk
93/231
-
8/8/2019 Final XML Ppt - Bk
94/231
-
8/8/2019 Final XML Ppt - Bk
95/231
Task 4: Identify the performingcalculations.
(Contd.) round(num): Rounds up the number to the
nearest integer.Result
As per the scenario, the sum() function of
XPath needs to be used for calculating theproduct sales value of each product. Thecode for calculating the product sales valuesis given as follows:
- =' *
-
8/8/2019 Final XML Ppt - Bk
96/231
Task 5:C
reate an XSLT style sheetcontaining XPath
patterns and functions.
Task 6: Create the XML document.
Task 7: View the XML document.
-
8/8/2019 Final XML Ppt - Bk
97/231
Problem Statement 6.D.3The details about the books that are available forsale at CyberShoppe are stored in an XMLdocument. The book details, such as book ID, title,
rate, author first name, and author last name shouldbe displayed in a tabular format. The first name andlast name of an author should be displayed in asingle column, "AUTHOR(S)". If a book has multiple
authors, their names should be displayed as comma-separated values.
-
8/8/2019 Final XML Ppt - Bk
98/231
Task List
Identify the data to be displayed.
Identify a mechanism to be used to display
the data in a tabular format. Identify the elements required to display
data in a tabular format.
Identify a mechanism to create a comma-separated list of values.
Create a style sheet.
Apply the style sheet to the XML
-
8/8/2019 Final XML Ppt - Bk
99/231
Task 1: Identify the data to be displayed.Result
In the given scenario, the data to be
displayed is as follows: BOOKID
TITLE
RATE AUTHOR
FIRSTNAME
LASTNAME
-
8/8/2019 Final XML Ppt - Bk
100/231
Task 2: Identify a mechanism to be used todisplay
the data in a tabular format.
Information pertaining to the way in whichXML data must appear in a browser isspecified in either CSS or XSLT style sheets.
However, in certain situations, these twomethods do not have the capability to displaydata in certain formats.
You can combine the features of HTML and
-
8/8/2019 Final XML Ppt - Bk
101/231
-
8/8/2019 Final XML Ppt - Bk
102/231
-
8/8/2019 Final XML Ppt - Bk
103/231
Task 3: Identify the in a tabular format.(Contd.)
Result:
In the given scenario, you can use theTABLE, TR, TH, and TD elements in an XSLTstyle sheet to display data in a tabular format.
-
8/8/2019 Final XML Ppt - Bk
104/231
Task 4: Identify a mechanism to create acomma-
separated list of values.
Result
In order to create a comma-separated list ofvalues, you can use either the template orthe for-each element.
-
8/8/2019 Final XML Ppt - Bk
105/231
Task 5:Create a style sheet.
Task 6:Apply the style sheet to the XMLdocument.
Task 7:View the XML document.
-
8/8/2019 Final XML Ppt - Bk
106/231
-
8/8/2019 Final XML Ppt - Bk
107/231
Creating Nested Style SheetsThe import Element
The import element is an element supported
by XSLT in which one XSLT style sheet canbe reused by another XSLT style sheet.
The general form of the import element is as
follows:
The import element is a top level element that
must appear immediately after the stylesheet
-
8/8/2019 Final XML Ppt - Bk
108/231
-
8/8/2019 Final XML Ppt - Bk
109/231
Summary (Contd.) XPath expressions can match specific
patterns, retrieve results, and performadditional operations relative to the context of
the returned nodes. XPath provides functions, which can be
categorized as follows: String functions: Used for basic string operations,
such as finding a string's length or changing astring from uppercase to lowercase.
Node-set functions: Used to manipulate nodesets or return information about node sets.
Boolean functions: Return either true or false
-
8/8/2019 Final XML Ppt - Bk
110/231
Summary (Contd.) You can use HTML code in an XSLT style
sheet to display data in different formats.
The import element is used to import anXSLT style sheet to another XSLT stylesheet.
-
8/8/2019 Final XML Ppt - Bk
111/231
Objectives
In this lesson, you will learn to:
Identify the need for XML DocumentObject Model (DOM)
Use the following XML DOM objects to
access different parts of an XMLdocument:
DOMDocument
-
8/8/2019 Final XML Ppt - Bk
112/231
The XML Document Object Model
The Document ObjectModel(DOM)defines the
logical structure of documents and theway adocument is accessed and manipulated. It
provides anApplication Programming Interface (API)for XML andHTML documents.
-
8/8/2019 Final XML Ppt - Bk
113/231
-
8/8/2019 Final XML Ppt - Bk
114/231
The XML Document Object Model
(Contd.)
A DOM-enabled parser reads an XMLdocument and parses it to ensure that it isa valid document. Then, it creates an in-memory representation of the XML
document by organizing its contents in alogical tree-structure. The tree-structure ismade up of nodes.
MSXML is an example of a DOM-enabled
-
8/8/2019 Final XML Ppt - Bk
115/231
Implementation ofDOM in the MSXML
Parser When the MSXML parser loads an XML
document, it reads the document andcreates a tree structure that represents thevarious components of the XML document.
-
8/8/2019 Final XML Ppt - Bk
116/231
Implementation ofDOM in the MSXML
Parser
(Contd.)
The following diagram depicts how DOMallows applications to access informationstored in XML documents:
XML
Document
MSXMLLibrary
Parsed
Document
ParserDOM ree
Root
Child
text
Child
text
Application
-
8/8/2019 Final XML Ppt - Bk
117/231
Tree structure of a Document
The basic building block of the treestructure is the node.
Nodes are generic containers that holdinformation about the elements, attributes,content, comments, and processing
instructions that are stored in an XMLdocument.
The XML document can be viewed as a
single node that contains all the other
-
8/8/2019 Final XML Ppt - Bk
118/231
Tree structure of a Document
Every component of the XML document isrepresented as a node in the DOM tree-
structure. Each node has a name, type, and value
associated with it. The name of the node
is the name of the component with whichthe node is associated. The type of thenode depends on the type of thecomponent it represents.
-
8/8/2019 Final XML Ppt - Bk
119/231
XML DOM Objects and Methods The main objects and methods provided
by XML DOM that enable you to traverse,
read, and manipulate the structure andcontent of an XML document are listedbelow:The DOMDocument object
The IXMLDOMNode object
The IXMLDOMNodeList object
The IXMLDOMParseError object
-
8/8/2019 Final XML Ppt - Bk
120/231
The DOMDocument object
The DOMDocument object is the top-level
object in XML DOM. It implements all the
basic DOM methods required to work withan XML document. It also has methods,which support XSLT.
This object is associated with variousmethods that help you navigate, query,and modify the content and the structureof an XML document.
-
8/8/2019 Final XML Ppt - Bk
121/231
The DOMDocument object (Contd.) Some of the most commonly used methods of the DOMDocument object
are:
createElement(elementname): Creates an element node.
createAttribute(attributename): Creates an attribute node.
CreateNode(type, name, namespace-URI): Creates a node of thespecified type. Also supports namespace URI.
getElementsByTagName(elementname): Returns a collection ofelement nodes that have the specified element name.
-
8/8/2019 Final XML Ppt - Bk
122/231
-
8/8/2019 Final XML Ppt - Bk
123/231
The DOMDocument object (Contd.)readyState: Returns the information about
the state of the XML document (loading,
loaded, interactive, complete).parseError: Returns an object of
IXMLDOMParseError, which can be used toretrieve error information.
xml: Returns the XML representation of anode.
validateOnParse: Specifies whether the
parser should check for validity of an XML
-
8/8/2019 Final XML Ppt - Bk
124/231
Creating a DomDocument object
The following is the code segment writtenusing JavaScript to create an instance of
the DOMDocument object:var myxmldoc = new
ActiveXObject("Msxml2.DOMDocum
ent.4.0"); A reference to the newly created object is
stored in the variable myxmldoc, which
can be used to load and manipulate XML
-
8/8/2019 Final XML Ppt - Bk
125/231
-
8/8/2019 Final XML Ppt - Bk
126/231
Loading an XML file (Contd.)
To load an XML document synchronously,you can write the following code in
JavaScript:var myxmldoc = new
ActiveXObject("Msxml2.DOMDocum
ent.4.0");myxmldoc.async=false;
myxmldoc.load("employee.xml");
-
8/8/2019 Final XML Ppt - Bk
127/231
Using the readyState Property
To check if the document has beenloaded completely, you can use the
readyState property. The readyStateproperty holds a numeric value, whichrepresents one of the following states:
LOADING (1): This state indicates that theloading process is in progress and data is notyet parsed.
LOADED (2): This state indicates that data
has been read and parsed but the object
-
8/8/2019 Final XML Ppt - Bk
128/231
Using the readyState Property(Contd.)
INTERACTIVE (3): This state indicates that the
object model is available with partially retrieveddata set and is in the read-only mode.
COMPLETED (4): This state indicates that theloading process is complete.
-
8/8/2019 Final XML Ppt - Bk
129/231
Creating a New Node
You can create a new node by using thecreateNode() method.
The following is the code segment tocreate a root element by using thecreateNode() method:
var myxmldoc = new
ActiveXObject("Msxml2.DOMDocument.4.0");
if
(m xmldoc.childnodes.len th==0)
-
8/8/2019 Final XML Ppt - Bk
130/231
Creating New Elements
You can create new elements for thedocument by using the
createElement() method of themyxmldoc DOMDocument object. ThecreateElement() method takes one
parameter, the name of the element thatis to be created.
The following example creates a newelement node called salary:
-
8/8/2019 Final XML Ppt - Bk
131/231
Accessing Elements From an XML File To access the elements from an XML
document, you can begin at the rootelement and navigate through thedocument tree. You can also query for aspecific node or nodes.
To reach a particular node or nodes within
the document tree structure, you can usethe getElementsByTagName() method.
-
8/8/2019 Final XML Ppt - Bk
132/231
The IXMLDOMNode object The IXMLDOMNode object represents a
node in the XML document tree structure. The node could be an element that
contains child elements. The IXMLDOMNode object provides
methods to work with child elements.
Some commonly used methods of theIXMLDOMNode object are: appendChild(newchild): Appends a new child
to the node. insertBefore(newNode, refNode): Inserts a
-
8/8/2019 Final XML Ppt - Bk
133/231
The IXMLDOMNode object (Contd.) replaceChild(newNode, oldNode): Replaces
the oldNode with newNode.
hasChildNodes(): Specifies whether the node
has child nodes. cloneNode(boolean): Creates a clone of the
node represented by the IXMLDOMNodeobject.
Some of the commonly used properties ofthe IXMLDOMNode object are:
nodeName: Returns the name of the node.
nodeType: Specifies the type of the node.
-
8/8/2019 Final XML Ppt - Bk
134/231
The IXMLDOMNode object (Contd.) firstChild: Returns the first child of the
node.
lastChild: Returns the last child of thenode.
text: Returns the text contained in thenode.
xml: Returns the XML code for the node.
-
8/8/2019 Final XML Ppt - Bk
135/231
Accessing Text Values of Elements
The text enclosed within the tags in anXML file is used as a node value in DOM.
A node value can be the value of anattribute or the text within an element.
You can display the text within the
elements or attributes by using the textproperty, as shown below:
alert(myelement.text);
You can also set the value of an element
-
8/8/2019 Final XML Ppt - Bk
136/231
Appending a New Child Node
The following code segment creates anew element by using the
createElement() method of theDOMDocument object. Then, it appends
the newly created node to the last child ofmyelement by using theappendchild() method:
var
myelement=myxmldoc.documentEle
-
8/8/2019 Final XML Ppt - Bk
137/231
Inserting a Node Before Two ExistingNodes
The following code segment creates a
new element called department andreplaces and existing node with the newelement:
var newElement=myxmldoc.createElement
("department");
var
-
8/8/2019 Final XML Ppt - Bk
138/231
Removing a Child Node The following code segment removes a
child node by using the removeChild()method:
varoldElement=myxmldoc.documentElement.childNodes.item(0).first
Child;myxmldoc.documentElement.childNodes.item(0).RemoveChild(oldElement);
-
8/8/2019 Final XML Ppt - Bk
139/231
Replacing a Node (Contd.) In the following example, the second
element in the document is replaced with
the new element named department:Var newElement=
myxmldoc.createElement("depart
ment");Var
oldElement=myxmldoc.documentEl
ement.childNodes.item(0).first
-
8/8/2019 Final XML Ppt - Bk
140/231
TheIXMLDOMNodeList object
The IXMLDOMNodeList object enables you to iterate through a collection
of nodes.
Some methods of the IXMLDOMNodeList object are:
item(number):Returns the node at the position indicated by thespecified number.
nextNode(): Returns the next node in the list.
To obtain the length of the node list, use the length property. Thelength property can also be used to traverse through the list of child
nodes.
-
8/8/2019 Final XML Ppt - Bk
141/231
The IXMLDOMNodeList object (Contd.) The following code traverses through the
child nodes ofmyelement:
varmyelement=myxmlDoc.getElements
ByTagName("emp");
for(i=0;i< myelement.length 1;i++)
alert(myelement.item(i).xml);
-
8/8/2019 Final XML Ppt - Bk
142/231
The IXMLDOMParseError Object The IXMLDOMParseError object returns
information about the most recent parse
error. The IXMLDOMParseError object
provides properties to retrieve
information, such as the error code, theerror text, and the line that caused theerror.
-
8/8/2019 Final XML Ppt - Bk
143/231
TheIXMLDOMPa
rseErrorO
bject (C
ontd.) The properties of the IXMLDOMParseError object are:
errorCode: Returns the error code.
reason: Returns a string explaining the reason for the error.
line: Returns the line number where the error occurred.
linePos: Returns the position in the line where the error occurred.
srcText: Returns a string containing the line that caused the error.
-
8/8/2019 Final XML Ppt - Bk
144/231
Using the IXMLDOMParseError object You can use the IXMLDOMParseError
object to display information about theerrors that are encountered while parsingan XML document.
Consider the following example:
var prodxml = new
ActiveXObject("Msxml2.DOMDocument.4.0");prodxml.async =false;
prodxml.load("product.xml");
-
8/8/2019 Final XML Ppt - Bk
145/231
Problem Statement 7.D.1
The product details of CyberShoppe arestored in an XML document. The structure
of the XML document is defined in a DTD.The data held in the XML document mustbe validated against the rules specified for
the data store.
-
8/8/2019 Final XML Ppt - Bk
146/231
Task List
Identify a mechanism to access an XMLdocument programmatically.
Identify the objects required to access theXML document.
Write the code to access the document.
Execute the code.
-
8/8/2019 Final XML Ppt - Bk
147/231
Task 1: Identify a mechanism to accessan XML
document programmatically.
Result: The contents of an XML document can be
accessed and manipulated from any
application by using XML DOM.Therefore, to access an XML documentprogrammatically, the best solution is touse XML DOM. Applications developed
-
8/8/2019 Final XML Ppt - Bk
148/231
Task 2: Identify the objects required toaccess
the XML document.
Result: You can validate the contents of an XML
document against a DTD by using the
following XML DOM objects: DOMDocument
IXMLDOMParseError
-
8/8/2019 Final XML Ppt - Bk
149/231
Task 3: Write the code to access the
document
To write a script that validates an XML
document against the DTD, you need tofollow the steps given below:
Create the user interface for accepting the
XML file name. Write the script to load the named XML
document.
Write the script to validate the XML document
-
8/8/2019 Final XML Ppt - Bk
150/231
Task 4: Execute the script
-
8/8/2019 Final XML Ppt - Bk
151/231
Just a Minute
The details about products sold atCyberShoppe are stored in an XML
document called product.xml. Write thecode to display the price of all products byusing DOM objects.
-
8/8/2019 Final XML Ppt - Bk
152/231
Summary
In this lesson, you learned that:
DOM is an application programminginterface that enables an application toaccess the contents of an XML document.
DOM objects enable you to access andmanipulate XML documents.
When the MSXML parser loads an XMLdocument, it creates a tree structure that
-
8/8/2019 Final XML Ppt - Bk
153/231
Summary (Contd.)
The basic building block of the treestructure is a node. A node is a container
that holds information about the elements,attributes, and content stored in an XMLdocument.
Some XML DOM objects that are used tomanipulate data in a document are: DOMDocument
IXMLDOMNode
-
8/8/2019 Final XML Ppt - Bk
154/231
Summary (Contd.)
The DOMDocument object is the top-level
object in XML DOM. This object provides
various properties and methods that helpyou to navigate, query, and modify thecontent and structure of XML documents.
The IXMLDOMNode object represents anode in the XML document structure. Thisobject provides methods to work with childelements.
-
8/8/2019 Final XML Ppt - Bk
155/231
Objectives
In this lesson, you will learn to:
Use XML DOM objects to perform thefollowing tasks:
Validate an XML document against an XMLschema
Provide user-selected view of data stored inan XML document
-
8/8/2019 Final XML Ppt - Bk
156/231
Problem Statement 8.D.1The head office of CyberShoppe receives data in theform of XML documents from its branches. In orderto ensure consistency of data sent by the branches,
the head office maintains the definitions forstructures of documents in schemas. On receivingdata from branches, the head office needs to verifythat the data conforms to the schema for the
respective documents. For this, they need to write ascript, which validates the data stored in an XMLdocument against a schema.
-
8/8/2019 Final XML Ppt - Bk
157/231
Task List Identify a mechanism to validate an XML
document against a schema.
Identify the objects required to access anXML document through a scriptinglanguage.
Write the code to validate an XMLdocument against the schema.
Execute the code.
-
8/8/2019 Final XML Ppt - Bk
158/231
Task 1: Identify a mechanism to validate anXML
document against a schema.
In order to validate an XML document, youneed to use a validating parser. MSXML is aDOM-based validating parser.
Result
You can use the XML DOM objects providedby MSXML in order to ensure that thestructure of the XML document conforms to
-
8/8/2019 Final XML Ppt - Bk
159/231
-
8/8/2019 Final XML Ppt - Bk
160/231
Task 2: Identify the scriptinglanguage.
(Contd.)
The XMLSchemaCacheO
bject The XMLSchemaCache object is used to
hold a collection of schema documents thatspecify the rules to which XML documentsmust conform.
-
8/8/2019 Final XML Ppt - Bk
161/231
-
8/8/2019 Final XML Ppt - Bk
162/231
-
8/8/2019 Final XML Ppt - Bk
163/231
Task 3: Write the code to validate theXML
document against the schema.
To write a script that validates an XMLdocument against the schema, you need tofollow the steps given below:
Create the user interface to accept thename of the XML document and the XMLschema.
Write the code to load the XML document.
-
8/8/2019 Final XML Ppt - Bk
164/231
Task 4: Execute the code.
-
8/8/2019 Final XML Ppt - Bk
165/231
Just a Minute
Write the code to add an XML schema called Products.xsd to aschema collection.
-
8/8/2019 Final XML Ppt - Bk
166/231
Problem Statement 8.D.2CyberShoppe sells its products through an e-commerce Web site. Details about the products soldat CyberShoppe need to be displayed. Product
details include product name, description, price, andquantity for all products. A customer can choose toview the product details either as a table or as abulleted list.
-
8/8/2019 Final XML Ppt - Bk
167/231
Task List Identify a mechanism to access an XML
document programmatically.
Identify the objects required to apply thestyle sheet to the XML document duringruntime.
Create the XML document.
Create the style sheets.
Write the code to apply a style sheetdynamically to an XML document.
-
8/8/2019 Final XML Ppt - Bk
168/231
T k 2 Id tif th bj t i d t
-
8/8/2019 Final XML Ppt - Bk
169/231
Task 2: Identify the objects required to
apply the stylesheet to the XML document during runtime.
The XSLTemplate Object
The XSLTemplate object is the DOM objectthat is used to access an XSLT style sheet.
This object is used to hold a cached stylesheet that can then be dynamically associatedwith an XML document.
Before a style sheet document can be appliedto an XML document, it is converted into a tree
structure b the arser.
-
8/8/2019 Final XML Ppt - Bk
170/231
Task 2: Identify the objects
runtime.(Contd.)
The IXSLProcessor Object
To process an XML document by using a stylesheet, you must create an instance of theIXSLProcessor object.
This object is used to apply a style sheet to an
XML document and then process thatdocument.
-
8/8/2019 Final XML Ppt - Bk
171/231
Task 2: Identify the objects
runtime.(Contd.)
Result
In the given scenario, the product detailscontained in the XML document need to bedisplayed either as a table or as a list by usingan XSLT style sheet. This can be done using
the XSLTemplate and IXSLProcessorobjects.
T k 3 C h XM d
-
8/8/2019 Final XML Ppt - Bk
172/231
Task 3: Create the XML document.
Task 4: Create the style sheets.
Task 5: Write the code to apply a style
sheet
dynamically to an XML document.
Task 6: Execute the code.
-
8/8/2019 Final XML Ppt - Bk
173/231
Just a Minute
The following JavaScript code is used to create XSLTemplateand DOMDocument objects. Identify the errors in the code, if any.
custss= new activexObject(MSXML.XSLTemplate.4)
custdomdoc= activexobject("MSXML.ThreadedDOMdocument.4.0);
-
8/8/2019 Final XML Ppt - Bk
174/231
Just a Minute
Identify the errors in the following JavaScript code:
xslProcObject.output=xmlDocObject;
xslProcObject.Transform
alert("xslProcObject");
-
8/8/2019 Final XML Ppt - Bk
175/231
Problem Statement 8.P.1CyberShoppe sells books through an e-commerceWeb site. A customer should be able to view bookdetails, such as the name of the book, the first andlast names of the author, the price, and the numberof available books. A customer should be given thechoice of viewing the book details either as a table oras a list. This choice is to be provided in a drop-down list. Also ensure that the XML document is
loaded only once in the browser.Hint: You can create a global xmldoc variable andload the XML document in it. Use the OnLoad eventof the BODY element to load the XML document.
S
-
8/8/2019 Final XML Ppt - Bk
176/231
Summary
In this lesson, you learned that:
The XMLSchemaCache object is used to
associate an XML document with an XSD
document. The XMLSchemaCache object is used to hold
a collection of schema documents that
specify the rules to which XML documentsmust conform.
The XSLTemplate object is used to contain
a com iled XSL document.
Summary (Contd )
-
8/8/2019 Final XML Ppt - Bk
177/231
Summary (Contd.)
The IXSLProcessor object is created using
the createProcessor() method.
The IXSLProcessor object is associated
with an XML document by using the inputproperty of the IXSLProcessor object.
The IXSLProcessor object provides the
transform() method to transform an XMLdocument according to the informationprovided in an XSLT style sheet.
-
8/8/2019 Final XML Ppt - Bk
178/231
Objectives
In this lesson, you will learn to:
Generate an XML document by using
data stored in SQL Server
Execute XPath queries against datastored in a SQL Server database
Insert XML data in a SQL Serverdatabase table
-
8/8/2019 Final XML Ppt - Bk
179/231
Getting Started SQL Server 2000 has introduced features
for integrating XML with SQL Server.
Some of these features are:
XPath queries
Direct URL queries
The OpenXML function
The FOR XML clause
-
8/8/2019 Final XML Ppt - Bk
180/231
Problem Statement 9.D
.1The details about the products sold at CyberShoppeare stored in a SQL Server 2000 database. This dataneeds to be displayed in a Web browser.
-
8/8/2019 Final XML Ppt - Bk
181/231
Task List Identify the mechanism for generating an
XML document.
Build the query for generating the XMLdocument.
Execute the query.
T k 1 Id tif th h i f
-
8/8/2019 Final XML Ppt - Bk
182/231
Task 1: Identify the mechanism forgenerating
an XML document.
SQL Server provides the SQL-XML
mapping rules for extracting XML datafrom a database and storing XML data inthe database.
The FOR XML clause of the SELECT
statement is used to extract data from adatabase and display it as an XMLdocument.
You can retrieve data in three different
T k 1 Id tif th XML d t
-
8/8/2019 Final XML Ppt - Bk
183/231
Task 1: Identify the XML document.
(Contd.)
The syntax for using the FOR XML clausein the SELECT statement is as follows:
SELECT table1.column_name1,table2.column_name2
FROM table1, table2
FOR XML mode
Result
You can use the FOR XML clause of the
-
8/8/2019 Final XML Ppt - Bk
184/231
Task 2: Build the query for generatingthe XML
document.
You need to create a URL query todisplay the XML document in a browser.
A URL query allows you to embed aSELECT statement along with the URL ofa Web server to generate an XMLdocument with the required data.
The syntax of a URL query is as follows:
-
8/8/2019 Final XML Ppt - Bk
185/231
Task 2: Build the XML document.(Contd.)
Result
To display the data stored in a SQLServer 2000 database, you need tocreate a URL query. To be able toexecute the URL query, you need to
create a Web site and map the virtualdirectory of the Web site to the SQLServer database.
-
8/8/2019 Final XML Ppt - Bk
186/231
Task 3: Execute the query.
-
8/8/2019 Final XML Ppt - Bk
187/231
Just a MinuteWhat are the three modes that can beused with the FOR XML clause?
J
-
8/8/2019 Final XML Ppt - Bk
188/231
Just a MinuteThe books table contains the detailsabout the books sold at CyberShoppe.Write a query to generate an XMLdocument based on the data stored in thebooks table. Also ensure that all columnsfrom the table are displayed as elements
and not attributes.
J
-
8/8/2019 Final XML Ppt - Bk
189/231
Just a MinuteIdentify the error in the following URLquery.
http://localhost/EMPLOYEES?sql=SELECT+*+FROM+EMPLOYEES FOR XML
AUTO&root=root
Problem Statement 9 D 2
-
8/8/2019 Final XML Ppt - Bk
190/231
Problem Statement 9.D.2
The details about the toys sold atCyberShoppe need to be displayed in a Webbrowser. The details are stored in the
products table of the CyberShoppe databaseon SQL Server 2000. The CATEGORY columnof the products table indicates the category ofa product.
-
8/8/2019 Final XML Ppt - Bk
191/231
Task List Identify the method for converting only
selective data.
Create an XML schema. Build a query.
Execute the query.
Task 1: Identify the method for
-
8/8/2019 Final XML Ppt - Bk
192/231
Task 1: Identify the method for
converting onlyselective data.
SQL Server 2000 allows you to generate anXML document based on the data stored in adatabase.
You can use the FOR XML clause of theSELECT statement to generate an XMLdocument and display it in a browser.
You can use XPath query to restrict theresults returned in an XML document.
XPath query is an XML-based query language
that can be used to rocess the data in an
Task 1: Identify the method data
-
8/8/2019 Final XML Ppt - Bk
193/231
Task 1: Identify the method data.
(Contd.)
Result
To display only selective nodes from theresulting XML document, you can useXPath query. XPath queries use XMLschemas for determining the structure of
the XML document and traversingthrough various nodes. Therefore, youneed to create an XML schema. SinceSQL Server 2000 supports XDR schema
-
8/8/2019 Final XML Ppt - Bk
194/231
Task 2: Create an XML schema
-
8/8/2019 Final XML Ppt - Bk
195/231
Task 2: Create an XML schema.
(Contd.)
The ElementType Element
Allows you to create an element type thatcan be used in a schema.
The syntax for the ElementType elementof XDR is as follows:
Task 2: Create an XML schema
-
8/8/2019 Final XML Ppt - Bk
196/231
Task 2: Create an XML schema.
(Contd.)
The AttributeType Element
Allows you to declare an attribute type,which can be used later in the schema.
The syntax for the AttributeType
element of XDR is as follows:
Task 2: Create an XML schema
-
8/8/2019 Final XML Ppt - Bk
197/231
Task 2: Create an XML schema.
(Contd.)
The element Element
Refers to an element type declared
earlier in the schema by using theElementType element.
Is used to declare the content model for
another element type. The syntax for this element is as follows:
-
8/8/2019 Final XML Ppt - Bk
198/231
Task 2: Create an XML schema.
(Contd.)
The attribute Element
Refers to an attribute type declaredearlier in the schema by using theAttributeType element.
Is used to declare the content model for
another element type. The syntax for this element is as follows:
-
8/8/2019 Final XML Ppt - Bk
199/231
Task 3: Build a query.
Task 4: Execute the query.
J
-
8/8/2019 Final XML Ppt - Bk
200/231
Just a Minute
The books sold at CyberShoppe aredivided in to various categories, such asScience, Fiction, and Biography. The
details about these books are stored in aSQL Server 2000 database. Create anXPath query for displaying the details
about all fiction books. Also create aschema required for executing the query.
-
8/8/2019 Final XML Ppt - Bk
201/231
-
8/8/2019 Final XML Ppt - Bk
202/231
Task List Identify the data that needs to be
inserted.
Identify the method for inserting XML datainto a database.
Build a query.
Execute the query.
Task 1: Identify the data that needs to
-
8/8/2019 Final XML Ppt - Bk
203/231
Task 1: Identify the data that needs to
be
inserted.
Result
As per the scenario, the data that needsto be inserted into the SALES table is thesales details of the various products. The
sales details includes the following: ORDER
SHIP_ADD
PRICE PER TY
Task 2: Identify the method for
-
8/8/2019 Final XML Ppt - Bk
204/231
Task 2: Identify the method for
inserting XMLdata into a database.
The OPENXML function is used to
retrieve the data from an XML documentas a rowset.
SQL Server 2000 provides the followingsystem stored procedures that can be
used with the OPENXML function. sp_xml_preparedocument
sp_xml_removedocument
Result
-
8/8/2019 Final XML Ppt - Bk
205/231
Task 3: Build a query.
Task 4: Execute the query.
J
-
8/8/2019 Final XML Ppt - Bk
206/231
J
ust a Minute What are the system stored procedures
that can be used with the OPENXMLfunction?
Summary
-
8/8/2019 Final XML Ppt - Bk
207/231
y
In this lesson, you learned that:
You can display data from a SQL Server 2000 database in a Web browser by using
the SQL-XML integration feature. To host a Web page, you need to install
Internet Information Server (IIS) on a
Windows 2000 or a Windows NT Server 4.0system. If you have Windows NT Workstation4.0 installed on a system, you can usePersonal Web Server (PWS) instead of IIS.
Summary (Contd.)
-
8/8/2019 Final XML Ppt - Bk
208/231
y ( )
The FOR XML clause forms a part of the T-SQL statement in a URL query.
In a URL query, http:/// should precede the SQLstatement.
The white spaces in the SELECT statement
of URL Query must be substituted by the +sign.
An XPath query is an XML-based query
lan ua e that can be used to rocess the
-
8/8/2019 Final XML Ppt - Bk
209/231
ObjectivesIn this lesson, you will learn to:
Identify the types of links in XML
documents Identify the need for XPointer for
referencing specific sections within an
XML document
-
8/8/2019 Final XML Ppt - Bk
210/231
Use of Hyperlinks A hyperlinkorlinkis an association
between a source document and a target
document, or between a piece of text andan object, such as an image.
When clicked, hyperlinks enable you to
move from one page to another or fromone location to another location on thesame page.
-
8/8/2019 Final XML Ppt - Bk
211/231
Use of Hyperlinks (Contd.)
Hyperlinks in HTML have the followinglimitations:
Using hyperlinks in HTML, you can point atonly a single document.
Links are one way. The linking document
knows to what it is linking, but the targetdocument does not know how it is linked.
To overcome these limitations ofhyperlinks in HTML, W3C recommends
-
8/8/2019 Final XML Ppt - Bk
212/231
Introduction to XLink XLinkstands for XML Linking Language. It
is a specification for enhanced and
improved linking capabilities that aredesigned for use with XML documents.
XLink allows XML documents to establish
a linking relationship between more thanone document and create linkingdocuments that reside in a separatelocation from the linked documents.
-
8/8/2019 Final XML Ppt - Bk
213/231
Introduction to Xlink (Contd.) XLinks are of two types:
Simple links
Extended links
-
8/8/2019 Final XML Ppt - Bk
214/231
Simple Links A simple link is similar to the link created
in HTML using the ANCHOR element. Itconnects an element in a sourcedocument to a target document.
Simple links are inline links. An inline linkis a part of the element and can connect in
one direction. A simple link can have only one resource
identifier or locator that contains dataabout the link.
-
8/8/2019 Final XML Ppt - Bk
215/231
-
8/8/2019 Final XML Ppt - Bk
216/231
Extended Links An extended link can point to several
resources at a time. It can be used to
create links to other documents from yourdocuments, even though you have no writeprivileges in the other documents.
Extended links associate an XML element
to multiple links.
-
8/8/2019 Final XML Ppt - Bk
217/231
Extended Links (Contd.) Extended links are of two types:
Out-of-line links
A link that is described by a resource but which isnot one of the links in the resource is called anout-of-line link.
An example is a catalog document that describesall the links in a Web site without actuallyparticipating in any of the links.
-
8/8/2019 Final XML Ppt - Bk
218/231
Extended Links (Contd.) Multi-directional links
An extended link may also be a multi-directionallink that joins several documents in a single link,
which can be traversed from any one of itsresources. Extended links enable you to specifymultiple links from one source.
For example, you might want to provide multiple
links to various topics, such as information aboutMichelangelo and Leonardo da Vinci from a singlecategory named Artists.
-
8/8/2019 Final XML Ppt - Bk
219/231
Behaviour of XLink XLink uses the following attributes for
creating simple or extended links:
href role
title
show
actuate
type
C
-
8/8/2019 Final XML Ppt - Bk
220/231
Behaviour of Xlink (C
ontd.) The href attributeA linking element must have a resource
locator, that is, the resource targeted by the
link. The href attribute of XLink is used to specify
the target of the link.
The role attribute The role attribute is a means of providing
applications with additional information abouta link.
Applications that use XLink can get the
-
8/8/2019 Final XML Ppt - Bk
221/231
Behaviour of Xlink (Contd.) The title attribute
The title attribute allows you to specify a
label to provide information to the user.
While the role attribute is meant for thesystem and the application, the title
attribute is meant for providing supplementary
information to the user.
-
8/8/2019 Final XML Ppt - Bk
222/231
Behaviour of Xlink (Contd.) The show attribute
The show attribute can take any one of thethree values, namely, embed, replace, ornew.
The behaviors represented by these valuesare described in the table below:
Value Behavior
embed If this value is specified, the contents of the linked object willappear
embedded within the document from which this link was activated.
replace If thisv
alue is specified,the content of the linked object willreplace thedocument from which the link was activated. This is the default behavior of
HTMLlinks.
new If this value is specified, the content of the linked object willappear separate
from the document from which the link was activated, such as in a new
browser window.
-
8/8/2019 Final XML Ppt - Bk
223/231
Behaviour of Xlink (Contd.) The actuate attribute
The actuate attribute allows you to specify
the timing of the link.
The actuate attribute can take one of the
following predefined values:Value Behavior OnReques
t
This specifies that the link should be traversed onlywhen the user requests it
byclicking on the linking element.
OnLoadThe link is tra
versed once the link is loaded. For example
,you mayset theactuate attribute to OnLoadforan image that is to be embedded in the
linking document. The link willbe traversedafter the image is loaded.
-
8/8/2019 Final XML Ppt - Bk
224/231
Behaviour of Xlink (Contd.) The type attribute
The type attribute is used to specify the type
of the link that is to be created for an element. The value of this attribute can be set tosimple, extended, resource, locator, orarc.
simple
A simple link is similar to an HTML hyperlink. Itconnects an element in a source document to a
-
8/8/2019 Final XML Ppt - Bk
225/231
Behaviour of Xlink (Contd.) extended
When you set the value of the type attribute toextended, it allows you to create multi-directional
links between many documents. resource
XML allows you to create multi-directional links inwhich you can have an element that can be both
the source as well as the target of a link. The source and the target have been given the
generic name resource.
A resource can be either local or remote. A local
C
-
8/8/2019 Final XML Ppt - Bk
226/231
Behaviour of Xlink (C
ontd.) locator
This link type is used to represent remoteresources.
A remote resource exists outside the extended linkelement.
arc
An arc describes a traversal path between twolinks.
-
8/8/2019 Final XML Ppt - Bk
227/231
Introduction to Xpointer XPointer, the XML Pointer Language,
defines an addressing scheme for theindividual parts of an XML document.
It can be used by any application thatneeds to identify a part or a location of anXML document.
XPointer is a new specification designed tolink to portions of a document withouthaving to link to the entire document.
-
8/8/2019 Final XML Ppt - Bk
228/231
Just a Minute Which of the following elements cannot be
displayed as a link?
Buttons Text
Menu Items
List Items
-
8/8/2019 Final XML Ppt - Bk
229/231
Just a Minute Which of the following is a type of
extended link?
In-line link Straight-line link
Out-of-line link
One-line link
-
8/8/2019 Final XML Ppt - Bk
230/231
SummaryIn this lesson, you learned that:
XLink stands for XML Linking Language. It
is a specification for enhanced linkingcapabilities designed for use with XMLdocuments.
XLinks are of two types. These are: Simple links
Extended links
-
8/8/2019 Final XML Ppt - Bk
231/231
Summary (Contd.)
The following attributes can be used tospecify the behavior of simple or extended
links: href
role