cp3024 lecture 9
DESCRIPTION
CP3024 Lecture 9. XML revisited, XSL, XSLT, XPath, XSL Formatting Objects. XML Revision. Say Bye Bye , Sweep Bye Bye, Sweep . Well formed XML. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/1.jpg)
1
CP3024 Lecture 9
XML revisited, XSL, XSLT, XPath, XSL Formatting Objects
![Page 2: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/2.jpg)
2
XML Revision
<?xml version="1.0"?><sweepjoke><harry>Say <quote>Bye Bye </quote>, Sweep </harry>
<sweep> <quote>Bye Bye, Sweep</quote></sweep>
<laughter/></sweepjoke>
![Page 3: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/3.jpg)
3
Well formed XML
Begins with XML declarationHas one unique root elementNon-empty elements have closing tagsEmpty elements terminatedTags appear in correct orderAttribute values quoted
![Page 4: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/4.jpg)
4
Valid XML
Valid documents are well formedConform to a DTD
– Correct sequence– Correct nesting– Valid attribute values
![Page 5: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/5.jpg)
5
Document Type Definition
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE sweepjoke [<!ELEMENT sweepjoke (harry+, sweep, laughter?)>
<!ELEMENT harry (#PCDATA | quote)*><!ELEMENT sweep (#PCDATA | quote)*><!ELEMENT quote (#PCDATA)*><!ELEMENT laughter EMPTY>]>
![Page 6: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/6.jpg)
6
Validating Parser
![Page 7: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/7.jpg)
7
Not Well Formed
<sweepjoke>
<harry>Say <quote>Bye Bye </quote>, Sweep </harry>
<sweep><quote>Bye Bye, Sweep</sweep></quote>
<laughter/>
</sweepjoke>
![Page 8: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/8.jpg)
8
Invalid 1
<sweepjoke>
<quote>Hello</quote>
<harry>Say <quote>Bye Bye </quote>, Sweep </harry>
<sweep><quote>Bye Bye, Sweep</quote></sweep>
<laughter/>
</sweepjoke>
![Page 9: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/9.jpg)
9
Invalid 2
<sweepjoke>
<sweep><quote>Bye Bye, Sweep</quote></sweep>
<laughter/>
</sweepjoke>
![Page 10: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/10.jpg)
10
Invalid 3
<sweepjoke>
<harry>Say <quote>Bye Bye </quote>, Sweep </harry>
<sweep><quote>Bye Bye, Sweep</quote></sweep>
<sweep>Hello Sooty</sweep>
<laughter/>
</sweepjoke>
![Page 11: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/11.jpg)
11
Is This Valid? 1
<sweepjoke>
<harry>Hello Sooty</harry>
<harry>Say <quote>Bye Bye </quote>, Sweep </harry>
<sweep><quote>Bye Bye, Sweep</quote></sweep>
<laughter/>
</sweepjoke>
![Page 12: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/12.jpg)
12
Is This Valid? 2
<sweepjoke>
<harry>Say <quote>Bye Bye </quote>, Sweep </harry>
<sweep><quote>Bye Bye, Sweep</quote></sweep>
<harry>Hello Sooty</harry>
<laughter/>
</sweepjoke>
![Page 13: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/13.jpg)
13
Namespaces in XML
An XML namespace is a collection of names– Identified by a URI reference [RFC2396]– Used in XML documents as element types and
attribute names
![Page 14: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/14.jpg)
14
Namespace Definition
<x xmlns:edi='http://ecommerce.org/schema'>
<!-- the "edi" prefix is bound to
http://ecommerce.org/schema for the "x" element and contents
-->
</x>
![Page 15: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/15.jpg)
15
Namespace use
<x xmlns:edi='http://ecommerce.org/schema'> <!-- the 'price' element's namespace is
http://ecommerce.org/schema -->
<edi:price units='Euro'>32.18</edi:price>
</x>
![Page 16: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/16.jpg)
16
Structured Publishing
Taken from an example given by Jon Bosak
![Page 17: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/17.jpg)
17
XSL
eXtensible Stylesheet LanguageThree parts
– XSLT• Language for transforming XML documents
– XPath• Language for defining parts of an XML document
– XSL Formatting Objects• Vocabulary for formatting XML documents
![Page 18: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/18.jpg)
18
XSLT
XSL TransformationsTransforms from an XML source tree to an
XML result treeTypically used to transform XML to
XHTML
![Page 19: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/19.jpg)
19
Transformation Process
Use XPath to define parts of the document that match predefined templates
On match, transform source into resultUnmatched parts of the source copied
unmodified to result
![Page 20: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/20.jpg)
20
Browser Support
IE 5.0 and 5.5 not 100% compatibleIE 6.0 supports all W3C recommendationsFully supported in Netscape 7.xFully supported in Mozilla 1.3
![Page 21: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/21.jpg)
21
Demo XML File
<?xml version = "1.0"?>
<moduleList>
<module no="CP3024">
<name>Web Information Systems</name>
<comment>Easy!</comment>
</module>
<module no="CP2027">
<name>Prog in Java</name>
<comment>Hard</comment>
</module>
</moduleList>
![Page 22: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/22.jpg)
22
Required Output
![Page 23: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/23.jpg)
23
Untransformed View
![Page 24: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/24.jpg)
24
HTML Output
<html><head><title>ModuleList<title></head><body><table border="1" bgcolor="cyan">
<thead><tr>
<th>Module Number</th><th>Name</th><th>Information</th>
</tr></thead>
![Page 25: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/25.jpg)
25
HTML Output
<tbody><tr>
<td>CP3024</td><td>Web Information Systems</td><td>Easy!</td>
</tr><tr>
<td>CP2027</td><td>Prog in Java</td><td>Hard</td>
</tr></tbody></table></body></html>
![Page 26: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/26.jpg)
26
Style Sheet Declaration
XSL stylesheets are written as XMLFirst line identifies file as XMLSecond line should be:
– <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”>
![Page 27: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/27.jpg)
27
XSL Template
The <xsl:template> element contains rules to apply when a specified node is matched
The match attribute is used to associate the template with an XML element
E.g. <xsl:template match = "/">
Use of / selects root (whole document)Closes with </xsl:template>
![Page 28: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/28.jpg)
28
Example Stylesheet
<?xml version = "1.0"?><xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"><xsl:template match = "/"> <html><head>
<title>Module List</title></head><body>
<table border = "1" bgcolor = "cyan">
![Page 29: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/29.jpg)
29
Example Stylesheet
<thead><tr><th>Module Number</th><th>Name</th><th>Information</th>
</tr></thead>
</table></body></html></xsl:template></xsl:stylesheet>
![Page 30: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/30.jpg)
30
Output
![Page 31: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/31.jpg)
31
Applying A Stylesheet
<?xml version = "1.0"?><?xml-stylesheet type = "text/xsl" href="moduleTable.xsl" ?><moduleList> <module no="CP3024"> <name>Web Information Systems</name> <comment>Easy!</comment> </module> <module no="CP2027"> <name>Prog in Java</name> <comment>Hard</comment> </module></moduleList>
![Page 32: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/32.jpg)
32
XML Tree Representation
n am e com m ent
m od u le
m od u le L ist
![Page 33: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/33.jpg)
33
XPath
Identifies parts of an XML documentCan select a node
– moduleSelect direct descendant
– moduleList/moduleSelect any descendant
– moduleList//commentSelect attribute
– @no
![Page 34: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/34.jpg)
34
XSL For-Each
Used to select every node of a specified setE.g <xsl:for-each select = "moduleList/module">
The select attribute contains an Xpath expression
It defines the context for the enclosed expressions
![Page 35: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/35.jpg)
35
XSL Value-Of
Selects the value of an XML elementAdds it to the outputE.g.
<xsl:value-of select = "name"/>The select attribute contains an Xpath expressionThis determines what is added to the output
![Page 36: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/36.jpg)
36
Outputting Elements
<tbody><xsl:for-each select = "moduleList/module"><tr>
<td><xsl:value-of select = "@no"/></td><td><xsl:value-of select = "name"/></td><td><xsl:value-of select = "comment"/></td>
</tr></xsl:for-each>
</tbody>
![Page 37: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/37.jpg)
37
Output
![Page 38: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/38.jpg)
38
Applying A Filter
Can use for-each statement to select entriesE.g.
<xsl:for-each select = "moduleList/module[@no='CP3024']">
![Page 39: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/39.jpg)
39
Sorting Output
Can use xsl:sort
<xsl:for-each select = "moduleList/module"> <xsl:sort select = "name"/> <tr> <td><xsl:value-of select = "@no"/></td> <td><xsl:value-of select = "name"/></td> <td><xsl:value-of select = "comment"/></td> </tr></xsl:for-each>
![Page 40: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/40.jpg)
40
XSL:Sort
Select attribute indicates which attribute to sort on
![Page 41: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/41.jpg)
41
XSL Formatting Objects
Source: W3C
![Page 42: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/42.jpg)
42
XSLT Transform
Source: W3C
![Page 43: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/43.jpg)
43
Formatting
Source: W3C
![Page 44: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/44.jpg)
44
Final Formatting
Source: W3C
![Page 45: CP3024 Lecture 9](https://reader036.vdocuments.net/reader036/viewer/2022070416/568151dd550346895dc015af/html5/thumbnails/45.jpg)
45
Summary
XML revisited– Well formed– Valid
XSL– XSLT– XPath– XSL Formatting Objects