320 likes | 415 Views
XML Technology in E-Commerce. Lecture 6 XPointer, XSLT. Lecture Outline. XPointer Purpose: specifying fragment identifiers for XML documents; Points and Ranges; Extensions to XPath; XPointer Forms; XSLT Rendering XML; XSL Suite; Example; XSLT Elements;. Overview.
E N D
XML Technology in E-Commerce Lecture 6 XPointer, XSLT
Lecture Outline • XPointer • Purpose: specifying fragment identifiers for XML documents; • Points and Ranges; • Extensions to XPath; • XPointer Forms; • XSLT • Rendering XML; • XSL Suite; • Example; • XSLT Elements;
Overview Software Application SAX DOM Parser XML Languages XLink XSLT XPointer XML Schema XPath XML Namespaces XML
Fragment Identifiers • The part after “#” character in URIs is called fragment identifier: http://some.site.net/path/myFile.html#introduction; http://some.site.net/path/myXML.xml#id001’ • Fragment identifier specifies retrieval action on the resource; • The syntax and interpretation of fragment identifiers are media dependent;
XPointer • Specifies language for expressing fragment identifiers for XML documents; • Addresses internal structures of XML documents; • Extends XPath; • Provides facilities for: • selection of a specific position in an XML document; • selection of a range in an XML document; • Current status: Working Draft; http://www.w3.org/TR/xptr
competition results photos name name name img img Range John Smith D. Warwick M. Douglas Point 1 Container: photos Index: 0 Point 2 Container: photos Index: 2 Range Point 1 Container: Text Index: 5 Point 2 Container: Text Index: 2 Points and Ranges DOM representation of a sample XML document:
XPointer Terms • Point - position in XML information. Consists of container node and index; • Range - all the information between two end points; • Location - XPath’s nodes plus points and ranges; • Location set - ordered list of locations. Usually a result of XPointer expression; Location and Location set are a generalization of XPath’s Node and Node set
Extensions to XPath • XPath allows locating subset of nodes; • XPointer allows locating parts that are not nodes. Points and Ranges extend the notion of Node; • XPointer expression yields object of type location set; • Evaluation is made relatively to the root node of an XML document. • The test for points and ranges uses point and range keywords;
XPointer Forms • Full XPointers: xpointer(//contact[@id=“author02”]) xlink:href=“/contacts.xml#xpointer(//contact[@id=‘author02’])” • Bare names: short syntax for id() function: xpointer(id(“author02”)) xlink:href=“/contacts.xml#author02”
XPointer Functions • range-to(expr). The start point is the context location. The end point is returned by the evaluation of expr; xpointer(id("chap1")/range-to(id("chap2"))) • string-range(location-set, string). For each location in location-set, the function returns string ranges that match the string argument; string-range(//title,”Introduction")[4]
Summary on XPointer • Specifies fragment identifiers in URIs; • Still unstable, working draft status; • Based on XPath; • Implementations: • Fujitsu XLink Processor; • libxml; • 4XPointer;
Lecture Outline • XPointer • Purpose; • Points and Ranges; • Extensions to XPath; • XPointer Forms; • XSLT • Rendering XML; • XSL Suite; • Example; • XSLT Elements;
Rendering XML • HTML specifies how the documents are rendered; • In general, XML-based languages do not specify how the documents are rendered; Problem: How to render XML documents on different devices? XML Document
Rendering XML • Solutions: • Attach CSS stylesheet; • Transformation in another document language understood by a particular device; CSS Stylesheet Browser FO Document PDF HTML XML Document Transformation WML ?
XSL • XSL - Extensible Stylesheet Language. Language for expressing stylesheets; • Two parts: • XSLT (Extensible Stylesheet Language for Transformations): Language for specifying transformations of XML documents. W3C Recommendation; • XSL Formatting Objects (XSL FO): XML vocabulary for expressing formatting semantics. W3C Candidate Recommendation; • XSL W3C site: http://www.w3.org/Style/XSL/
Example • What we have: <book isbn="999-99999-9-x"> <title>Deitel XML Primer</title> <author> <firstName>Paul</firstName> <lastName>Deitel</lastName> </author> <chapters> <preface num="1" pages="2">Welcome</preface> <chapter num="1" pages="4">Easy XML</chapter> <chapter num="2" pages="2">XML Elements?</chapter> <appendix num="1" pages="9">Entities</appendix> </chapters> <media type="CD"/> </book>
Example • What we want: <html> <head> <title>ISBN - 999-99999-9-x - Deitel XML Primer</title> </head> <body> <h1>Deitel XML Primer</h1> <h2>Deitel, Paul</h2> <table border="1"> <tr><td align="right">Preface 1</td> <td>Welcome (2 pages )</td></tr> <tr><td align="right">Chapter 1</td> <td>Easy XML (4 pages )</td></tr> <tr><td align="right">Chapter 2</td> <td>XML Elements? (2 pages )</td></tr> <tr><td align="right">Appendix 1</td> <td>Entities (9 pages )</td></tr> </table> </body> </html>
XSLTBasic Terms • Transformation expressed in XSLT is called stylesheet; • Stylesheet transforms a source tree into a result tree; • Stylesheet is a set of template rules; • Rules have two parts: • Pattern: selects nodes from the source tree. Uses XPath syntax; • Template: instantiated in order to form a part of the source tree; • Source and result tree roughly conform to the XPath data model;
XSLTGeneral Processing Scheme Stylesheet expressed in XSLT XSLT Processor Result tree Source tree
XSLTRule matching and Template Instantiation Matches Instantiates Rule 1 Matches Rule 2 Matches Instantiates Rule 3 Source tree Result tree Stylesheet
Demo • Demo - modified version of Deitel 12.5, fig. 12.8 and 12.9, page 327; • Tools: • XML Spy 3.5; • XSLT Processor: • MSXML 3 • Or any downloadable extension for Spy, e.g. Infoteria iXSLT or SAXON; • Demo files: • book.xml; • style.xsl;
Demo Explained • Stylesheet root element: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> ………………………………………………………….. </xsl:stylesheet> • Stylesheet contains elements from XSLT namespace. They are recognized and executed by the XSLT processor; • Stylesheet includes non-XSLT elements. They can reside in their own namespace;
Template Rules • First, the root node of the source tree is mapped to html element, which is the root element of all HTML documents, i.e. the root node of the result tree: <xsl:template match="/"> <html> <xsl:apply-templates/> </html> </xsl:template> • <xsl:apply-templates/>processes all children of the current node. In our case the current node is the root node;
Getting Text from Source Tree • Element value-of: <xsl:template match="book"> <head> <title>ISBN - <xsl:value-of select="@isbn"/> - <xsl:value-of select="title"/></title> </head> <body> <h1><xsl:value-of select="title"/></h1> <h2><xsl:value-of select="author/lastName"/>, <xsl:value-of select="author/firstName"/></h2> ………………………………………………… ………………………………………………… </body> </xsl:template>
Repetition • Creating the table and the first row: <xsl:template match=“book”> ………… <table border = "1"> <xsl:for-each select = "chapters/preface"> <tr><td align = "right">Preface <xsl:value-of select = "@num"/></td> <td><xsl:value-of select = "."/> (<xsl:value-of select = "@pages"/> pages )</td> </tr> </xsl:for-each> • for-each element instantiates the template for each node selected by the pattern. The selected node becomes the current node for the template;
Creating Elements • We can reformulate a part of the previous template by using element XSLT element: <tr><td align = "right">Preface <xsl:value-of select = "@num"/></td> <xsl:element name=“td”> <xsl:value-of select = "."/> (<xsl:value-of select = "@pages"/> pages ) </xsl:element> </tr> • The template contained in the element, becomes the content of the element with name td in the result tree;
Creating Attributes • We can reformulate a part of the previous template by using attribute XSLT element: <tr><td align = "right">Preface <xsl:value-of select = "@num"/></td> <td><xsl:value-of select = "."/> (<xsl:value-of select = "@pages"/> pages ) </td> </tr> <tr> <xsl:element name=“td”> <xsl:attribute name=“align”>right</xsl:attribute> Preface <xsl:value-of select = "@num"/> </xsl:element> <td><xsl:value-of select = "."/> (<xsl:value-of select = "@pages"/> pages ) </td> </tr>
Copying Nodes • XSLT element copy copies the current node and includes it in the result tree; • The attributes and children are not copied; • The template of the copy element generates their content; • Example - identity transformation: <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
DemoUsing XSLT to test the XPath expressions • Demo - uses the file from Deitel Ex11.3, fig. 11.15, page 316; • Tools: • XML Spy 3.5; • XSLT Processor: • MSXML 3 • Or any downloadable extension for Spy, e.g. Infoteria iXSLT or SAXON; • Demo files: • transactions.xml; • xpath.xsl;
Processing Model • At each step we have a list of source nodes; • At the first step the list contains the root node; • Each node is processed by finding the template rules with pattern that matches the node; • Only one rule is selected; • The template rule is instantiated with the node as a current node; • XSLT instructions in the template are executed. They add new source nodes for processing; • The process stops when the list of source nodes is empty;
Summary on XSLT • Powerful transformation language for XML documents based on XML syntax; • Allows multiple representations of a single document; • Available software implementations: • MSXML 3; • iXSLT for Infoteria; • Saxon; • Xalan-Java, Xalan-C++, part of Apache XML Project; • XT;
Summary on XSLT (2) Read: Deitel 12; Assignment: Deitel Ex 12.3, Ex 12.7, page 351. For some hints see the course web site.