450 likes | 536 Views
CP3024 Lecture 9. XML revisited, XSL, XSLT, XPath, XSL Formatting Objects. XML Revision. <?xml version="1.0"?> <sweepjoke> <harry>Say <quote>Bye Bye </quote>, Sweep </harry> <sweep> <quote>Bye Bye, Sweep</quote></sweep> <laughter/> </sweepjoke>. Well formed XML.
E N D
CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects
XML Revision <?xml version="1.0"?> <sweepjoke> <harry>Say <quote>Bye Bye </quote>, Sweep </harry> <sweep> <quote>Bye Bye, Sweep</quote></sweep> <laughter/> </sweepjoke>
Well formed XML • Begins with XML declaration • Has one unique root element • Non-empty elements have closing tags • Empty elements terminated • Tags appear in correct order • Attribute values quoted
Valid XML • Valid documents are well formed • Conform to a DTD • Correct sequence • Correct nesting • Valid attribute values
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> ]>
Not Well Formed <sweepjoke> <harry>Say <quote>Bye Bye </quote>, Sweep </harry> <sweep><quote>Bye Bye, Sweep</sweep></quote> <laughter/> </sweepjoke>
Invalid 1 <sweepjoke> <quote>Hello</quote> <harry>Say <quote>Bye Bye </quote>, Sweep </harry> <sweep><quote>Bye Bye, Sweep</quote></sweep> <laughter/> </sweepjoke>
Invalid 2 <sweepjoke> <sweep><quote>Bye Bye, Sweep</quote></sweep> <laughter/> </sweepjoke>
Invalid 3 <sweepjoke> <harry>Say <quote>Bye Bye </quote>, Sweep </harry> <sweep><quote>Bye Bye, Sweep</quote></sweep> <sweep>Hello Sooty</sweep> <laughter/> </sweepjoke>
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>
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>
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
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>
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>
Structured Publishing Taken from an example given by Jon Bosak
XSL • eXtensible Stylesheet Language • Three parts • XSLT • Language for transforming XML documents • XPath • Language for defining parts of an XML document • XSL Formatting Objects • Vocabulary for formatting XML documents
XSLT • XSL Transformations • Transforms from an XML source tree to an XML result tree • Typically used to transform XML to XHTML
Transformation Process • Use XPath to define parts of the document that match predefined templates • On match, transform source into result • Unmatched parts of the source copied unmodified to result
Browser Support • IE 5.0 and 5.5 not 100% compatible • IE 6.0 supports all W3C recommendations • Fully supported in Netscape 7.x • Fully supported in Mozilla 1.3
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>
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>
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>
Style Sheet Declaration • XSL stylesheets are written as XML • First line identifies file as XML • Second 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”>
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>
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">
Example Stylesheet <thead> <tr> <th>Module Number</th> <th>Name</th> <th>Information</th> </tr> </thead> </table> </body></html> </xsl:template> </xsl:stylesheet>
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>
XPath • Identifies parts of an XML document • Can select a node • module • Select direct descendant • moduleList/module • Select any descendant • moduleList//comment • Select attribute • @no
XSL For-Each • Used to select every node of a specified set • E.g <xsl:for-each select = "moduleList/module"> • The select attribute contains an Xpath expression • It defines the context for the enclosed expressions
XSL Value-Of • Selects the value of an XML element • Adds it to the output • E.g. <xsl:value-of select = "name"/> • The select attribute contains an Xpath expression • This determines what is added to the output
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>
Applying A Filter • Can use for-each statement to select entries • E.g. <xsl:for-each select = "moduleList/module[@no='CP3024']">
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>
XSL:Sort • Select attribute indicates which attribute to sort on
XSL Formatting Objects Source: W3C
XSLT Transform Source: W3C
Formatting Source: W3C
Final Formatting Source: W3C
Summary • XML revisited • Well formed • Valid • XSL • XSLT • XPath • XSL Formatting Objects