Download - XML Technology in E-Commerce
Sheet 1XML Technology in E-Commerce 2001 Lecture 6
XML Technology in E-Commerce
Lecture 6
XPointer, XSLT
Sheet 2XML Technology in E-Commerce 2001 Lecture 6
• XPointer– Purpose: specifying fragment identifiers for XML
documents;
– Points and Ranges;
– Extensions to XPath;
– XPointer Forms;
• XSLT– Rendering XML;
– XSL Suite;
– Example;
– XSLT Elements;
Lecture Outline
Sheet 3XML Technology in E-Commerce 2001 Lecture 6
XML
XML Namespaces
XPath
XPointerXSLT
XLink
XML Schema
XML Languages
Parser
DOMSAX
Software Application
Overview
Sheet 4XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 5XML Technology in E-Commerce 2001 Lecture 6
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
Sheet 6XML Technology in E-Commerce 2001 Lecture 6
Points and Ranges
img
competition
results photos
imgname name name
John Smith D. Warwick M. Douglas
Point 1Container: TextIndex: 5
Range Point 2Container: TextIndex: 2
Range
Point 1Container: photosIndex: 0
Point 2Container: photosIndex: 2
DOM representation of a sample XML document:
Sheet 7XML Technology in E-Commerce 2001 Lecture 6
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
Sheet 8XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 9XML Technology in E-Commerce 2001 Lecture 6
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”
Sheet 10XML Technology in E-Commerce 2001 Lecture 6
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]
Sheet 11XML Technology in E-Commerce 2001 Lecture 6
Summary on XPointer
• Specifies fragment identifiers in URIs;• Still unstable, working draft status;• Based on XPath;• Implementations:
– Fujitsu XLink Processor;– libxml;
– 4XPointer;
Sheet 12XML Technology in E-Commerce 2001 Lecture 6
• XPointer– Purpose;
– Points and Ranges;
– Extensions to XPath;
– XPointer Forms;
• XSLT– Rendering XML;
– XSL Suite;
– Example;
– XSLT Elements;
Lecture Outline
Sheet 13XML Technology in E-Commerce 2001 Lecture 6
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?
XMLDocument
Sheet 14XML Technology in E-Commerce 2001 Lecture 6
Rendering XML• Solutions:
– Attach CSS stylesheet;
– Transformation in another document language understood by a particular device;
XMLDocument
CSSStylesheet Browser
Transformation
FODocument
HTML
WML
?
Sheet 15XML Technology in E-Commerce 2001 Lecture 6
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/
Sheet 16XML Technology in E-Commerce 2001 Lecture 6
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>
Sheet 17XML Technology in E-Commerce 2001 Lecture 6
• What we want:Example
<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>
Sheet 18XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 19XML Technology in E-Commerce 2001 Lecture 6
XSLTGeneral Processing Scheme
Stylesheetexpressed in XSLT
XSLTProcessor
Source treeResult tree
Sheet 20XML Technology in E-Commerce 2001 Lecture 6
Source tree Result tree
Instantiates
Instantiates
Stylesheet
XSLTRule matching and Template Instantiation
Rule 1
Rule 2
Rule 3
Matches
Matches
Matches
Sheet 21XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 22XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 23XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 24XML Technology in E-Commerce 2001 Lecture 6
Getting Text from Source Tree
<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>
•Element value-of:
Sheet 25XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 26XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 27XML Technology in E-Commerce 2001 Lecture 6
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>
Sheet 28XML Technology in E-Commerce 2001 Lecture 6
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>
Sheet 29XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 30XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 31XML Technology in E-Commerce 2001 Lecture 6
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;
Sheet 32XML Technology in E-Commerce 2001 Lecture 6
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.