Beruflich Dokumente
Kultur Dokumente
Welcome!
XML Day 1
XML Day 1
Day 1: Objectives
After completing this course, you should be able to: Define what is XML Identify the document type definitions and validity Describe attribute declarations in DTDs Explain entities and external DTD subsets Define embedding non XML data Describe XML namespaces and parsers Identify SAX parser
XML Day 1
Housekeeping
Breaks Washrooms Transportation / parking No pagers or cell phones
Participation
Parking lot issues Questions
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
XML Day 1
With both valid and well-formed XML, XML encoded data is self-describing since descriptive tags are intermixed with the data. DTDs help ensure that different people and programs can read each others files. The DTD defines exactly what is and is not allowed to appear inside a document.
XML Day 1
XML Day 1
DTD declarations
Element type declarations Attribute-list declarations Entity declarations Notation declarations
Processing declarations
Comments Parameter entity references
XML Day 1
<!ELEMENT General ANY> You can use any key word which is legal under General Root tag <!ELEMENT Image EMPTY> Element must be empty. It can not have anything <!ELEMENT Book (Title, Author, Publisher) Book element must have the 3 elements in the same order <!ELEMENT Prerequisite ( BE | ME | MS) Prerequisite element can have either only one of the above <!ELEMENT Candidate (Qualification+, XMLExposure?, OtherSkills*)
Qualification could be one or more, XMLExposure is optional and OtherSkills could be zero or more.
9 XML Day 1
Copyright IBM Corporation 2009
Example
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE Collection [ <!ELEMENT Collection (CD)+> <!ELEMENT CD (#PCDATA)> ] > <Collection> <CD>Devotional Songs by Pankaj</CD> <CD>Kajal by Pankaj</CD> <CD>Classical Songs by Pankaj</CD> </Collection>
10 XML Day 1
Copyright IBM Corporation 2009
11
XML Day 1
12
XML Day 1
13
XML Day 1
14
XML Day 1
Element declarations
Each tag used in a valid XML document must be declared with an element declaration in the DTD. This specifies the name and possible contents of an element. This list of contents is also called the content specification. * - may occur more than once (Zero or More Children) ? may or may not occur (Zero or One Children) + - must occur at least once (One or More Children)
15
XML Day 1
16
XML Day 1
CDATA sections
May contain text, reserved characters and whitespace
Reserved characters need not be replaced by entity references
Not processed by XML parser Commonly used for scripting code (e.g., JavaScript) Begin with <![CDATA[ Terminate with ]]>
17
XML Day 1
<?xml version = "1.0"?> <!-- Fig. 5.7 : cdata.xml <!-- CDATA section containing C++ code --> -->
<sample> // C++ comment if ( this->getX() < 5 && value[ 0 ] != 3 ) cerr << this->displayError(); </sample> <sample> <![CDATA[
// C++ comment if ( this->getX() < 5 && value[ 0 ] != 3 ) cerr << this->displayError(); Note the simplicity offered ]]> by CDATA section </sample> C++ How to Program by Deitel & Deitel </book>
XML Day 1
Copyright IBM Corporation 2009
19
XML Day 1
Example:
<!DOCTYPE SEASON SYSTEM http://ibm/xml/dtds/sample.dtd>
20
XML Day 1
Public DTDs
The SYSTEM keyword is intended for private DTDs used by a single author or group. DTDs designed for writers outside the creating organization use the PUBLIC keyword instead of SYSTEM keyword. <! DOCTYPE root_element_name PUBLIC DTD_name DTD_URL >
Example:
<! DOCTYPE HTML PUBLIC -//W3C//DTD HTML //EN >
21
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
22
XML Day 1
What is an Attribute?
Attributes are intended for extra information associated with an element (like an ID number) used only by programs that read and write the file, and not for the content of the element thats read and written by humans. The Attribute contains information about the content of the element, rather than the content itself. Example: <GREETING LANGUAGE=English> Hello XML! <MOVIE SOURCE=WavingHand.mov /> </GREETING> Attribute ( key = value)
23
XML Day 1
Examples
<RECTANGLE WIDTH=30 HEIGHT=45 /> <SCRIPT LANGUAGE=javascript ENCODING=8859_1>
.
</SCRIPT> Note: End Tags cannot possess Attributes. <SCRIPT>
24
XML Day 1
25
XML Day 1
26
XML Day 1
Attribute types
Type CDATA Enumerated ID IDREF IDREFS ENTITY ENTITIES
27 XML Day 1
Meaning Character Data text that is not markup A list of possible values from which exactly one will be chosen A unique name not shared by any other ID type attribute in the document The value of an ID type attribute of an element in the document Multiple IDs of elements separated by whitespace The name of an entity declared in the DTD The name of multiple entities declared in the DTD, separated by whitespace.
Copyright IBM Corporation 2009
28
XML Day 1
#REQUIRED
Instead of providing default values for the attributes, if you want to force anyone posting a document on the intranet to identify themselves, then we go for #REQUIRED. Example:
<!ELEMENT AUTHOR EMPTY>
29
XML Day 1
#IMPLIED
Sometimes you may not have a good option for a default value, but you do not want to require the author of the document to include a value, either. For example, some of the people posting documents to your intranet are offsite freelancers who have email addresses but lack phone extensions. Therefore, you dont want to require them to include an extension attribute in their <AUTHOR/> tags.
<AUTHOR NAME=Harish EMAIL=harish.modadugu@in.ibm.com/> <!ELEMENT AUTHOR EMPTY>
30
XML Day 1
#FIXED
Used to provide a default value for the attribute without allowing the author to change it. For Example:
<AUTHOR NAME=Harish COMPANY=IBM EMAIL=hmodadug@in.ibm.com EXTENSION=57536 /> <!ELEMENT AUTHOR EMPTY> <!ATTLIST AUTHOR NAME CDATA #REQUIRED>
31
XML Day 1
Some examples
<!ATTLIST Film Class CDATA >
Simple form of defining an attribute. Attribute contains characters
32
XML Day 1
Example
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE VideoLibrary [ <!ELEMENT VideoLibrary (Film, Class, (Hero | Director | Heroine)+)> <!ATTLIST Film Color CDATA #IMPLIED Language CDATA #FIXED "Hindi" Year CDATA #REQUIRED> <!ELEMENT Film (#PCDATA)> <!ELEMENT Class (#PCDATA)>
<VideoLibrary>
<Film Year = "1994"> Hum Aapke Hain Kaun </Film> <Class>Love Story </Class> <Heroine>Madhuri Dixit</Heroine> </VideoLibrary>
>
33 XML Day 1
Copyright IBM Corporation 2009
Predefined Attributes
XML has two predefined Attributes. They are identified by a name that begins with xml:.
xml:space describes how whitespace is treated in the element. xml:lang describes the language in which the element is written.
34
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
35
XML Day 1
What is an Entity?
The storage units that contain particular parts of an XML document are called entities. An entity may consist of a file, a database record, or any other item that contains data. The primary purpose of an entity is to hold content: well-formed, other forms of text, or binary data. A CSS style sheet is not an entity. Every XML has at least one entity.
36
XML Day 1
Kinds of Entities
External Entity
They draw their content from another source located via a URL. In HTML, an IMG element represents an external entity while the document itself contained between the <HTML> and </HTML> tags is an internal entity.
37
XML Day 1
38
XML Day 1
Example
<?xml version=1.0 standalone=yes?> <!DOCTYPE DOCUMENT [ <!ENTITY ELTP ENTRY LEVEL TRAINING PROGRAM> <!ELEMENT DOCUMENT (TITLE, COURSE)> <!ELEMENT TITLE (#PCDATA)> <!ELEMENT COURSE (COURSE_CODE, DATE)> <!ELEMENT COURSE_CODE (#PCDATA)> <!ELEMENT DATE (#PCDATA)>
]>
<DOCUMENT> <TITLE> &ELTP; </TITLE> <COURSE> <COURSE_CODE> HYD_010 </COURSE_CODE>
40
XML Day 1
Example
An XML signature file
<?xml version=1.0?>
<SIGNATURE>
<NAME> HARISH </NAME> <EMPNO> 034518 </EMPNO> </SIGNATURE>
Parameter Entities
General entities become part of the document, not the DTD. They can be used in the DTD but only in places where they become part of the document body. Parameter entity references differs from general entity references in the following:
Parameter entity references begin with a percent sign (%) rather than an ampersand (&).
Parameter entity references can only appear in the DTD, not the document content.
42
XML Day 1
Syntax
<!ENTITY % name replacement text> Example:
<!ENTITY % IBM International Business Machines> <!ENTITY ACRON IBM stands for %IBM;>
43
XML Day 1
44
XML Day 1
Example
Sign.dtd <!ELEMENT EMP (EMPNO, NAME)> <!ELEMENT EMPNO (#PCDATA)>
Emp.xml
<?xml version=1.0 standalone=no?> <!DOCTYPE EMP SYSTEM Sign.dtd> <EMP> <EMPNO> 12121 </EMPNO> <NAME> Akash </NAME> </EMP>
45
XML Day 1
We simply declare the entities we want as normal; then use them in our documents.
46
XML Day 1
<EMP>
<EMPNO> 112233 </EMPNO> <NAME> Abhishek </NAME> </EMP>
</DOCUMENT>
47
XML Day 1
External Entities
Sign.dtd <?xml version=1.0?> <EMP> A File that uses Sign.dtd
<?xml version=1.0 standalone=no?> <DOCTYPE DOCUMENT [ <!ENTITY % EMPS SYSTEM Sign.dtd> ]> <DOCUMENT> <TITLE> XML </TITLE>
&EMPS;
</DOCUMENT>
48
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
49
XML Day 1
Notations
The first problem that we encounter when working with non-XML data in an XML document is identifying the format of the data and telling the XML application how to read and display the non-XML data. For ex., it would be inappropriate to try to draw an MP3 sound file on the screen. Furthermore, no application understands all possible file formats.
Ideally, we want documents to tell the application the format of the external entity so you dont have to rely on the application recognizing the file type by a magic number or a potentially unreliable file formats.
50
XML Day 1
Notations
It is used to provide a fixed and mandatory value to an attribute. The value is declared in the notation which can have a path using SYSTEM or a string using PUBLIC.
51
XML Day 1
Notations
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT IMAGES (IMAGE+) > <!ELEMENT IMAGE (#PCDATA) > <!NOTATION iPATH SYSTEM "C:\windows\a.bmp" >
52
XML Day 1
Using notations
<?xml version="1.0" encoding="UTF-8"?>
Note: Because XML processor cannot parse bmp files, we need to use an external program for displaying or editing them. When the parser encounters a usage of the notation line name, it will simply provide the path of the application.
53
XML Day 1
Conditional sections
Include declarations
Keyword INCLUDE
Exclude declarations
Keyword IGNORE
54
XML Day 1
55
XML Day 1
13 ]]> 14 15 <!ATTLIST <!ELEMENT approved flag EMPTY> 16 ( true | false ) "false"> 17 18 <!ELEMENT reason ( #PCDATA )>
56
56
XML Day 1
10
<signature>Chairman</signature>
11 </message>
57 XML Day 1
Copyright IBM Corporation 2009
58
XML Day 1
Processing instructions
A processing instruction is a string of text between <? And ?> marks. The only required syntax for the text inside the processing instruction is that it must begin with an XML name followed by white space followed by data. Note: Processing Instructions may be placed almost anywhere in an XML document except inside a tag or a CDATA section.
59
XML Day 1
version
A string in the form n.n specifying the XML level of the file. Use the value 1.0.
DTDflag Optional.
A Boolean value indicating whether the XML file includes a reference to an external Document Type Definition (DTD). Script component XML files do not include such a reference, so the value for this attribute is always "yes."
60
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
61
XML Day 1
Conflicting issues
Namespaces ensure that element names do not conflict, and clarify who defined which term. Namespaces do not give instructions on how to process the elements. Readers still need to know what the elements mean and decide how to process them.
62
XML Day 1
XML Namespaces
Naming collisions
Two different elements have same name
<subject>Math</subject>
<subject>Thrombosis</subject>
Namespaces
Differentiate elements that have same name <school:subject>Math</school:subject> <medical:subject>Thrombosis</medical:subject> school and medical are namespace prefixes Prepended to elements and attribute names Tied to uniform resource identifier (URI) Series of characters for differentiating names
63 XML Day 1
Copyright IBM Corporation 2009
Default namespaces
Child elements of this namespace do not need prefix xmlns = urn:deitel:textInfo
64
XML Day 1
XML Parsers are software packages that comes as part of an application or as part of our own programs.
There are two types of XML Parsers.
DOM Parser.
SAX Parser.
65
XML Day 1
66
XML Day 1
DOM (continued)
67
XML Day 1
DOM implementations
DOM-based parsers
Microsofts msxml
68
XML Day 1
XML4J
Xerces
msxml
4DOM
XML::DOM
69
XML Day 1
70
XML Day 1
Example
1 <?xml version = "1.0"?> 2 3 <!-- Fig.: article.xml --> 4 <!-- Article formatted with XML --> 5 6 <article> 7 8 <title>Simple XML</title> 9 10 <date>December 6, 2000</date> 11 12 <author> 13 <fname>Tem</fname> 14 <lname>Nieto</lname> Article marked up 15 </author> 16 with XML tags 17 <summary>XML is pretty easy.</summary> 18 19 <content>Once you have mastered HTML, XML is easily 20 learned. You must remember that XML is not for 21 displaying information but for managing 22 </content> information. 23 24 </article>
71 XML Day 1
Copyright IBM Corporation 2009
Example - DOM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
72
--> Element script allows for including scripting code Instantiate Microsoft XML DOM object
<script type = "text/javascript" language = "JavaScript"> var xmlDocument = new ActiveXObject( "Microsoft.XMLDOM" ); Load article.xml into memory; msxml parses article.xml and xmlDocument.load( "article.xml" ); stores it as tree structure
XML Day 1
Copyright IBM Corporation 2009
Example (continued)
21 22 23 24 25 26 27 28 29 30 document.writeln( "<br>The following are its child elements:" ); document.writeln( // get the root element var element = xmlDocument.documentElement; Assign article as root element Place root elements name in element strong and write it to browser
"<p>Here is the root node of the document:" ); document.writeln( "<strong>" + element.nodeName + "</strong>" );
31
32
73
document.writeln( "</p><ul>" );
XML Day 1
Example (continued)
33 34 35 36 37 38 39 40 41 42 43 44 45 46
74 XML Day 1
Copyright IBM Corporation 2009
// traverse all child nodes of root element for ( i = 0; i < element.childNodes.length; i++ ) { var curNode = element.childNodes.item( i ); Assign index to each child node of root node // print node name of each child element document.writeln( "<li><strong>" + curNode.nodeName + "</strong></li>" ); }
document.writeln( "</ul>" );
// get the first child node of root element var currentNode = element.firstChild;
Example (continued)
47 48 49 50 51 52 53 54 55 56 document.writeln( "<strong>" + nextSib.nodeName + "</strong>." ); document.writeln( "<p>The first child of root node is:" ); document.writeln( "<strong>" + currentNode.nodeName + "</strong>" ); document.writeln( "<br>whose next sibling is:" Siblings ); are nodes at same level in document (e.g., title, date, author, summary and content) // get the next sibling of first child Get first childs next sibling (date) var nextSib = currentNode.nextSibling;
75
XML Day 1
Example (continued)
57 58 59 60 61 62 63 64 65 // print the text value of the sibling document.writeln( "<em>" + value.nodeValue + "</em>" ); document.writeln( "<br>Parent node of " ); document.writeln( "<string>" + nextSib.nodeName var value = nextSib.firstChild; document.writeln( "<br>Value of <strong>" + nextSib.nodeName + "</strong> element is:" );
66
67 68 69
+ "</strong> is:" );
document.writeln( "<strong>" + nextSib.parentNode.nodeName + "</strong>.</p>" ); Get parent of date (article)
77
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
78
XML Day 1
XML schema
To define the structure of an XML document. defines the list of elements and attributes than can be used in an XML Document It also specifies the order in which these elements appear in the XML document and their datatypes
79
XML Day 1
80
XML Day 1
81
XML Day 1
82
XML Day 1
83
XML Day 1
Example:
<xs:element name=PRODUCTNAME type=xs:string/> <xs:element name=PRICE type=xs:positiveInteger/>
84
XML Day 1
</xs:restriction>
</xs:simpleType>
It defines a simple datatype called phoneno. The string value can be 10 character long and must match the pattern ddd-ddd-dddd.
85
XML Day 1
enumeration
86
XML Day 1
num is the user-defined simple datatype <xs:simpleType name="num"> <xs:restriction base="xs:positiveInteger"> <xs:maxInclusive value="400"/>
87
XML Day 1
88
XML Day 1
</xs:complexType>
89
XML Day 1
90
XML Day 1
Name : Specify the name of a user-defined attribute Ref: used to reference a user-defined attribute declared Type: It takes a value, which specified the datatype
example: Type=xs:string or type=myphone
91
XML Day 1
default :
<xs:attribute name=baseprice type=xs:integer use=default value=25 />
required
<xs:attribute name=baseprice type=xs:integer use=required />
Fixed
<xs:attribute name=baseprice type=xs:integer use=fixed value=600 />
92
XML Day 1
Global Attributes
Global attributes are attributes that are declared outside all elements declarations. They facilitate reusability of attributes We need to use <xs:schema> element as the parent element
<xs:schema>
93
XML Day 1
<xs:attribute ref=NAME/>
</xs:complexType>
94 XML Day 1
Copyright IBM Corporation 2009
95
XML Day 1
96
XML Day 1
All
attributeGroup
97
XML Day 1
Grouping elements
<xs:group name=empname>
<xs:sequence>
<xs:element name=FIRSTNAME type=xs:string/>
<xs:element name=LASTNAME type=xs:string/>
</xs:sequence>
<xs:element name=EMPLOYEE type=emptype/> <xs:complexType name=emptype> <xs:sequence> <xs:group ref=empname/> <xs:element name=ADDRESS type=xs:string/>
</xs:sequence>
</xs:complexType>
</xs:schema>
98
XML Day 1
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
99
XML Day 1
Introduction
XML Path Language (XPath) Syntax for locating information in XML document
e.g., attribute values
100
XML Day 2
Text
Comment Processing instruction Namespace
Attributes and namespaces are not children of their parent node but they describe their parent node.
101 XML Day 2
Copyright IBM Corporation 2009
Example - Nodes
1 <?xml version = "1.0"?> Root node 2 3 <!-- Fig.: simple2.xml --> Comment nodes 4 <!-- Processing instructions and namespacess --> 5 6 <html xmlns = "http://www.w3.org/TR/REC-html40"> 7 8 <head> 9 <title>Processing Instruction and Namespace Nodes</title> 10 </head> Namespace nodes 11 Processing instruction node 12 <?deitelprocessor example = "fig11_03.xml"?> 13 Element nodes 14 <body> 15 Text nodes 16 <deitel:book deitel:edition = "1" 17 xmlns:deitel = "http://www.deitel.com/xmlhtp1"> 18 <deitel:title>XML How to Program</deitel:title> 19 </deitel:book> 20 Attribute nodes 21 </body> 22 23 </html>
104 XML Day 2
Copyright IBM Corporation 2009
Namespace http://www.w3.org/TR/REC-html40
Element head Element title Text Processing instructions and Namespace Nodes
105 XML Day 2
Copyright IBM Corporation 2009
Processing Instruction deitelprocessor example = "fig.xml" Element body Element book Attribute edition 1 Namespace http://www.deitel.com/xmlhtp1 Element title Text XML How to Program
106 XML Day 2
Copyright IBM Corporation 2009
Determined by None. concatenating the string-values of all textnode descendents in document order. Determined by The element tag, concatenating the including the namespace string-values of all text- prefix (if applicable). node descendents in document order. The normalized value of the attribute. The name of the attribute, including the namespace prefix (if applicable).
element
attribute
107
XML Day 2
comment
The content of the comment None. (not including <!-- and -->).
The part of the processing instruction that follows the target and any whitespace.
108
XML Day 2
XPath example
XML document: <?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA"> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <price>10.90</price> </cd>
109
XML Day 2
XPath example
<cd country="UK"> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <price>9.90</price> </cd>
<cd country="USA">
<title>Greatest Hits</title> <artist>Dolly Parton</artist> <price>9.90</price> </cd> </catalog>
110 XML Day 2
Copyright IBM Corporation 2009
XPath expressions
To select the ROOT element catalog:
/catalog
To select all the price elements of all the cd elements of the catalog element:
/catalog/cd/price
Note: If the path starts with a slash (/) it represents an absolute path to an element
To select all the cd elements that have a price element with a value larger than 10.80:
/catalog/cd [price>10.80]
111
XML Day 2
Locating nodes
XML documents can be represented as a tree view of nodes XPath uses a pattern expression to identify nodes in an XML document. An XPath pattern is a slash-separated list of child element names that describe a path through the XML document. The pattern "selects" elements that match the path. The following XPath expression selects all the price elements of all the cd elements of the catalog element:
/catalog/cd/price
113
XML Day 2
To select all the price elements that are grandchild elements of the catalog element:
/catalog/*/price
114
XML Day 2
115
XML Day 2
Selecting branches
Square brackets in an XPath expression can specify an element further. To select the first cd child element of the catalog element:
/catalog/cd[1]
To select the last cd child element of the catalog element (Note: There is no
function named first()):
/catalog/cd[last()]
116
XML Day 2
To select all the cd elements of the catalog element that have a price element with a value of 10.90:
/catalog/cd[price=10.90]
To select all the price elements of all the cd elements of the catalog element that have a price element with a value > 10.90:
/catalog/cd[price>10.90]/price
117
XML Day 2
Selecting Attributes
In XPath all attributes are specified by the @ prefix. To select all attributes named country:
//@country
118
XML Day 2
To select all cd elements which have an attribute named country with a value of 'UK':
//cd[@country='UK']
119
XML Day 2
Course map
Module 1: Document type definitions and validity Module 2: Attribute declarations in DTDs Module 3: Entities and external DTD subsets
120
XML Day 1
121
XML Day 2
What is XSLT?
XSLT stands for XSL transformations XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses xpath to navigate in XML documents
122
XML Day 2
XSL consists of three parts: XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents
123
XML Day 2
XSL
124
XML Day 2
Presenting XML
There are two style sheet languages available for use with XML in Internet Explorer
Cascading Style Sheets (CSS) Extensible Style Language (XSL)
An important point to consider in choosing a style sheet language for a particular document is whether the structure of the XML document is suitable for display. With CSS, the structure of the XML content must be virtually identical to the structure of the presentation. Since one of the goals of XML is a complete separation of content from display, many XML documents are difficult to display as you might wish using CSS.
125
XML Day 2
126
XML Day 2
Books.XML
<?xml version="1.0"?> <!--DOCTYPE books SYSTEM "books.dtd"--> <?xml:stylesheet href="books.css" type="text/css"?> <books> <book> <title>Professional Active Server Pages 3.0</title> <authors> <author>Richard Anderson</author> <author>Chris Blexrud</author> <author>Andrea Chiarelli</author> <author>Dan Denault</author> </authors><price>us="$59.99"</price> </book></books>
127 XML Day 2
Copyright IBM Corporation 2009
Books.CSS
authors
{ display:block; fontfamily:Arial,Helvetica; font-style:italic; font-size:10pt; color:#990099;
price
{ display:block; border:2px solid black; padding:1em; background-color:#888833; color:#FFFFDD;
text-align:left;
}
font-weight:bold
margin-bottom: .4em; }
128
XML Day 2
XSL advantages
More sophisticated layout using HTML tables.
129
XML Day 2
XSL elements
There are various XSL Elements that can be used for applying the styles to the XML Document. Here is the list of few: xsl:for-each xsl:value-of xsl:if xsl:sort xsl:choose etc.
130
XML Day 2
Interview.XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href=Interview.xsl"?> <Interview xmlns:dt="urn:schemasmicrosoft-com:datatypes"> <Candidate> <Name>Mahesh</Name> <Project>Procter & Gamble</Project> <Score dt:dt="number">88</Score> </Candidate>
<Candidate>
<Name>Vishnu</Name> <Project>Banking</Project> <Score dt:dt="number">99</Score>
</Candidate>
<Candidate> <Name>Sridhar</Name> <Project>Telecom</Project>
<Score dt:dt="number">100</Score>
</Candidate> </Interview>
131
XML Day 2
Interview.XSL
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>Name</TD> <TD>Project</TD> <TD>Score</TD> </TR>
132
XML Day 2
Interview.XSL (continued)
<xsl:for-each select="Interview/Candidate"> <TR>
<TD><xsl:value-of select="Name"/></TD>
<TD><xsl:value-of select="Project"/></TD> <TD><xsl:value-of select="Score"/></TD> </TR> </xsl:for-each> </TABLE> </BODY>
</HTML>
</xsl:template> </xsl:stylesheet>
133 XML Day 2
Copyright IBM Corporation 2009
Xsl:for-each
<xsl:for-each order-by="sort-criteria-list select="pattern" > order-by
Sort criteria in a semicolon-separated list. When the first sort results in two equal items, the second sort criterion is checked, and so on. The first non-white-space character in each sort criterion indicates whether the sort is ascending (optional +) or descending (-). The sort criterion is expressed as an XSL pattern, relative to the pattern described in the select attribute.
select
XSL pattern query evaluated the current context to determine the set of nodes to iterate over. The default value "node()" indicates selection of all children of the current node.
134
XML Day 2
Xsl:for-each (continued)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/"> <HTML> <BODY>
<TABLE>
<xsl:for-each select="customers/customer order-by="name; -address/state"><TR> <TD><xsl:value-of select="name" /></TD>
135
XML Day 2
Xsl:value-of
Inserts the value of the selected node as text.
<xsl:value-of select="pattern" >
select
XSL pattern to be matched against the current context. The default value is ".", which inserts the value of the current node.
136
XML Day 2
138
XML Day 2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/"> <HTML> <BODY>
<TABLE BORDER="2">
<TR> <TD>Name</TD> <TD>Date of Birth</TD>
<TD>Project</TD>
<TD>Score</TD> </TR>
139 XML Day 2
Copyright IBM Corporation 2009
141
XML Day 2
142
XML Day 2
Mozilla
Mozilla includes Expat for XML parsing and has support to display XML + CSS. Mozilla also has some support for Namespaces. Mozilla is available with an XSLT implementation.
Netscape
As of version 8, Netscape uses the Mozilla engine, and therefore it has the same XML / XSLT support as Mozilla.
Opera
As of version 9, Opera has support for XML and XSLT (and CSS). Version 8 supports only XML + CSS.
Internet Explorer
As of version 6, Internet Explorer supports XML, Namespaces, CSS, XSLT, and Xpath. Version 5 is NOT compatible with the official W3C XSL Recommendation.
143 XML Day 2
Copyright IBM Corporation 2009
The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
144
XML Day 2
The most useful property of a tree is that each node and its children also form a tree. Thus, a tree is a hierarchical structure of trees in which each tree is built out of smaller trees.
145
XML Day 2
Each child element contains a node for its contents, as well as nodes for any attributes, comments and processing instructions it possesses.
Notice in particular that many nodes are something other than elements. There are nodes for text, attributes, comments, namespaces and processing instructions.
146
XML Day 2
XSLT transformation
The input must be an XML document XSLT can work with HTML and SGML documents XSLT is not a general-purpose regular expression language for transforming arbitrary data. The XSL transformation language contains operators for selecting nodes from the tree, reordering the nodes, and outputting nodes. Most of the time the output of an XSLT transformation is also an XML document. XSLT processors also support output as HTML and/or raw text, although the standard does not require them to do so.
147
XML Day 2
Text
Attributes Namespaces Processing instructions Comments
148
XML Day 2
149
XML Day 2
150
XML Day 2
<catalog>
<cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company> <price>10.90</price> <year>1985</year>
</cd>.
</catalog>
151 XML Day 2
Copyright IBM Corporation 2009
<body>
<h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th></tr>
152 XML Day 2
Copyright IBM Corporation 2009
153
XML Day 2
Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
154
XML Day 2
XSL templates
An XSL style sheet consists of one or more set of rules that are called templates. Each template contains rules to apply when a specified node is matched.
155
XML Day 2
156
XML Day 2
An example
<?xml version="1.0"?> <xsl:stylesheet version=1.0 xmlns:xsl="http://www.w3.org/1999/XSL/Transfor m"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr>
157 XML Day 2
Copyright IBM Corporation 2009
<tr>
<td>.</td>
<td>.</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet >
Explanation
Since an XSL style sheet is an XML document itself, it always begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>. The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes). The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document.
The content inside the <xsl:template> element defines some HTML to write to the output.
The last two lines define the end of the template and the end of the style sheet.
158
XML Day 2
159
XML Day 2
The <xsl:value-of>
The <xsl:value-of> element is used to extract the value of a selected node. It can be used to extract the value of an XML element and add it to the output stream of the transformation View cdcatalog_valueof.xsl View cdcatalog_valueof.xml
160
XML Day 2
The <xsl:for-each>
The <xsl:for-each> element allows you to do looping in XSLT. The element can be used to select every XML element of a specified nodeset: <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr> </xsl:for-each> Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.
161 XML Day 2
Copyright IBM Corporation 2009
162
XML Day 2
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>
163
XML Day 2
The <xsl:sort>
To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file: <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr> </xsl:for-each> Note: The select attribute indicates what XML element to sort on.
164
XML Day 2
165
XML Day 2
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each>
Note: The value of the required test attribute contains the expression to be evaluated. The code above will only output the title and artist elements of the CDs that has a price that is higher than 10.
166 XML Day 2
Copyright IBM Corporation 2009
Syntax <xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>
167
XML Day 2
To insert a multiple conditional test against the XML file, add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
<xsl:for-each select="catalog/cd"> <xsl:otherwise> <td> <xsl:value-of select="artist"/> </td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each>
<tr>
<td> <xsl:value-of select="title"/></td> <xsl:choose> 10">
<xsl:value-of select="artist"/>
</td> </xsl:otherwise> </xsl:choose>
</tr>
</xsl:for-each>
169 XML Day 2
Copyright IBM Corporation 2009
170
XML Day 2
171
XML Day 2
Wild cards
Sometimes you want a single template to apply to more than one element. You can indicate that a template matches all elements by using the asterisk wildcard (*) in place of an element name in the match attribute. For example this template says that all elements should be wrapped in a P element: <xsl:template match="*"> <P>
<xsl:value-of select="."/>
</P> </xsl:template> Of course this is probably more than you want.
Wed like to use the template rules already defined for PERIODIC_TABLE and ATOM elements as well as the root node and only use this rule for the other elements.
172 XML Day 2
Copyright IBM Corporation 2009
Matching by ID
We may want to apply a particular style to a particular single element without changing all other elements of that type. The simplest way to do that in XSLT is to attach a style to the element's ID type attribute. This is done with the id() selector, which contains the ID value in single quotes. For example, this rule makes the element with the ID e47 bold: <xsl:template match="id('e47')"> <b><xsl:value-of select="."/></b> </xsl:template>
173
XML Day 2
<xsl:template match="@UNITS">
<I><xsl:value-of select="."/></I> </xsl:template>
174
XML Day 2
Expression types
Every expression evaluates to a single value. there are five types of expressions in XSLT:
Node sets Booleans Numbers
Strings
Result tree fragments
175
XML Day 2
Node sets
A node set is an unordered group of nodes from the input document. The axes return a node set containing the nodes they match. Which nodes are in the node set depends on the context node, the node test, and the axis. For example, when the context node is the PERIODIC_TABLE element, the XPath expression
select="child::ATOM" returns a node set that contains both ATOM elements in that document.
select="child::ATOM/child::NAME" returns a node set containing the two element nodes <NAME>Hydrogen</NAME> and <NAME>Helium</NAME> when the context node is the PERIODIC_TABLE element.
176
XML Day 2
Context node
The context node is a member of the context node list. The context node list is that group of elements that all match the same rule at the same time, generally as a result of one xsl:apply-templates or xsl:for-each call.
177
XML Day 2
Return Type:
number number number node set
Returns:
The position of the context node in the context node list; the first node in the list has position 1 The number of nodes in the context node list; this is the same as the position of the last node in the list The number of nodes in node-set. A node set containing all the elements anywhere in the same document that have an ID named in the argument list; the empty set if no element has the specified ID. A node set containing all nodes in this document that have a key with the specified value. Keys are set with the top-level xsl:key element. A node set in the document referred to by the URI; the nodes are chosen from the named anchor or XPointer used by the URI. If there is no named anchor or XPointer, then the root element of the named document is the node set. Relative URIs are relative to the base URI given in the second argument. If the second argument is omitted, then relative URIs are relative to the URI of the style sheet (not the source document!).
178
XML Day 2
Return Type:
String
Returns:
The local name (everything after the namespace prefix) of the first node in the node set argument; can be used without any arguments to get the local name of the context node. The URI of the namespace of the first node in the node set; can be used without any arguments to get the URI of the namespace of the context node; returns an empty string if the node is not in a namespace.
namespace-uri(node set)
String
name(node set)
String
The qualified name (both prefix and local part) of the first node in the node set argument; can be used without an argument to get the qualified name of the context node. A unique identifier for the first node in the argument node set; can be used without any argument to generate an ID for the context node.
generate-id(node set)
String
179
XML Day 2
Position(): Example
The position() function can be used to determine an element's position within a node set. Prefixes the name of each atom's name with its position in the document using
<xsl:value-of select="position()"/>.
View periodictable_position.xsl
View periodictable_position.xml
180
XML Day 2
Booleans
A Boolean has one of two values: True or False. XSLT allows any kind of data to be transformed into a Boolean. This is often done implicitly when a string or a number or a node set is used where a Boolean is expected, as in the test attribute of an xsl:if element. These conversions can also be performed by the boolean() function which converts an argument of any type to a boolean according to these rules:
A number is false if it's zero or NaN (a special symbol meaning Not a Number, used for the result of dividing by zero and similar illegal operations); true otherwise. An empty node set is false. All other node sets are true. An empty result tree fragment is false. All other result tree fragments are true.
181
XML Day 2
Booleans (continued)
Booleans are also produced as the result of expressions involving these operators:
= equal to != not equal to < less than (really <) > greater than <= less than or equal to (really <=)
Note : The < sign is illegal in attribute values. Consequently, it must be replaced by < even when used as the less-than operator.
182
XML Day 2
Booleans (continued)
Child::ATOM selects all the ATOM children of the context node. Child::ATOM[position()=1] selects only the first ATOM child of the context node. [position()=1] is a predicate on the node test ATOM that returns a boolean result:
True if the position of the ATOM is equal to one; false otherwise.
Each node test can have any number of predicates. However, more than one is unusual.
183
XML Day 2
This template rule applies to all ATOM elements that are not the first child element of the PERIODIC_TABLE by testing whether the position is greater than 1:
<xsl:template match="PERIODIC_TABLE/ATOM[position()>1]"> <xsl:value-of select="."/>
</xsl:template>
184
XML Day 2
</xsl:template>
If the first condition is false, then the complete and expression is guaranteed to be false. Consequently, the second condition won't be checked. This template matches both the first and last ATOM elements in their parent by matching when the position is 1 or when the position is equal to the number of elements in the set:
<xsl:template match="ATOM[position()=1 or position()=last()]">
<xsl:value-of select="."/>
</xsl:template>
185 XML Day 2
Copyright IBM Corporation 2009
</xsl:template>
186
XML Day 2
Number functions
XPath numbers are 64-bit IEEE 754 floating-point doubles. Even numbers like 42 or -7000 that look like integers are stored as doubles.
Nonnumber values such as strings and booleans are converted to numbers automatically as necessary, or at user request through the number() function using these rules:
Booleans are 1 if true; 0 if false. A string is trimmed of leading and trailing white space, then converted to a number in the fashion you would expect;
For example: The string "12" is converted to the number 12. If the string cannot be interpreted as a number, then it is converted to the special symbol NaN, which stands for Not a Number.
Node sets and result tree fragments are converted to strings; the string is then converted to a number.
187 XML Day 2
Copyright IBM Corporation 2009
The node set produced by ATOMIC_NUMBER is implicitly converted to the string value of the current ATOMIC_NUMBER node. This string is then converted into a number.
<xsl:template match="/PERIODIC_TABLE"> <HTML> <HEAD> <TITLE>The Transuranium Elements</TITLE> </HEAD> <BODY> <xsl:apply-templates select="ATOM[ATOMIC_NUMBER>92]"/> </BODY>
</HTML> </xsl:template>
188
XML Day 2
189
XML Day 2
190
XML Day 2
191
XML Day 2
192
XML Day 2
193
XML Day 2
</HTML>
String functions
A string is a sequence of Unicode characters. Other data types can be converted to strings using the string() function according to these rules: Node sets are converted to strings by using the value of the first node in the set as calculated by the xsl:value-of element. A number is converted to a European-style number string like -12 or 3.1415292. Boolean false is converted to the English word false. Boolean true is converted to the English word true.
195
XML Day 2
String functions
Function: Return Type: Returns:
starts-with(main_string, prefix_string)
contains(containing_string, contained_string) substring(string, offset, length)
Boolean
Boolean String
String
String
Number String
196
XML Day 2
concat(string1, string2, . . . )
String
String
197
XML Day 2
This contains a number, which is rounded to the nearest integer, then formatted according to the value of the format attribute.
198
XML Day 2
<xsl:template match="PERIODIC_TABLE">
<html> <head><title>The Elements</title></head> <body> <table> <tr><xsl:apply-templates select="ATOM"/></tr> </table></body></html> </xsl:template> <xsl:template match="ATOM">
<td><xsl:number value="ATOMIC_NUMBER"/></td>
<td><xsl:value-of select="NAME"/></td> </xsl:template></xsl:stylesheet>
199 XML Day 2
Copyright IBM Corporation 2009
Default numbers
If you use the value attribute to calculate the number, that's all you need. However, if the value attribute is omitted, then the position of the current node in the source tree is used as the number.
200
XML Day 2
201
XML Day 2
xsl:number
We can change what xsl:number counts using these three attributes:
level count from
202
XML Day 2
xsl:number- count
<xsl:template match="ATOM/*"> <td> <xsl:number count="*"/> </td>
<td>
<xsl:value-of select="."/> </td> </xsl:template>
203
XML Day 2
xsl:number- level
By default, with no value attribute, xsl:number counts siblings of the source node with the same type. For instance, if the ATOMIC_NUMBER elements were numbered instead of ATOM elements, none would have a number higher than 1 because an ATOM never has more than one ATOMIC_NUMBER child. Although the document contains more than one ATOMIC_NUMBER element, these are not siblings. Setting the level attribute of xsl:number to any counts all of the elements of the same kind as the current node in the document. This includes not just the ones in the current node list, but all nodes of the same type. Even if you select only the atomic numbers of the gases, for example, the solids and liquids would still count, even if they weren't output. Consider these rules:
204
XML Day 2
<td><xsl:number level="any"/></td>
<td><xsl:value-of select="."/></td> </xsl:template>
205
XML Day 2
206
XML Day 2
Questions
207
XML Day 2
1. Which of the following is used to describe the XML document? a. Document Type Definition b. Data Type Definition c. Data Type Document d. Document Type Decision
208
XML Day 1
3. The wild card character used to describe 1 or many in DTD is _______. a. # b. + c. * d. ? 4. The storage unit that contain particular parts of XML document is called _____. a. b. c. d.
209
210
XML Day 1
b. True
211
XML Day 1
212
XML Day 1
Summary
At the completion of this course, we see that you are now able to: Put in your own words and introduction to XML Define what is XML Identify the document type definitions and validity Describe attribute declarations in DTDs Explain entities and external DTD subsets Define embedding non XML data Describe XML namespaces and parsers Explain XML schemas
Copyright IBM Corporation 2009
213
XML Day 1
THANK YOU
214
214
XML Day 2
Distribution Channels