Objectives Objectives � How XML documents may be rendered in An Introduction to XML and Web Technologies An Introduction to XML and Web Technologies browsers Transforming XML Documents Transforming XML Documents � How the XSLT language transforms XML with XSLT with XSLT documents � How XPath is used in XSLT Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley An Introduction to XML and Web Technologies 2 Presenting a Business Card Using CSS Presenting a Business Card Using CSS card { background-color: #cccccc; border: none; width: 300;} <card xmlns="http://businesscard.org"> name { display: block; font-size: 20pt; margin-left: 0; } <name>John Doe</name> title { display: block; margin-left: 20pt;} <title>CEO, Widget Inc.</title> email { display: block; font-family: monospace; margin-left: 20pt;} <email>john.doe@widget.inc</email> phone { display: block; margin-left: 20pt;} <phone>(202) 555-1414</phone> <logo uri="widget.gif"/> </card> � the information cannot be rearranged � information encoded in attributes cannot be exploited � additional structure cannot be introduced An Introduction to XML and Web Technologies 3 An Introduction to XML and Web Technologies 4 1
Using XSLT XSLT for Business Cards (1/2) Using XSLT XSLT for Business Cards (1/2) <?xml-styleshe <?xml-stylesheet et type="text/ type="text/xsl" href="bus xsl" href="businesscard.xsl" inesscard.xsl"?> ?> <xsl: <xs l:sty style lesh sheet eet ve vers rsion ion=" ="2. 2.0" 0" <card xmlns="http://businesscard.org"> xmlns xml ns:x :xsl= sl="ht "http tp:// ://ww www. w.w3. w3.org org/1 /1999 999/X /XSL SL/Tr /Trans ansfo form" rm" <name>John Doe</name> xml xmlns ns:b :b="h ="http ttp:/ ://bu /busi sine nessc sscard ard.o .org" rg" xml xmlns ns=" ="htt http:/ p://w /www. ww.w3 w3.o .org/ rg/199 1999/ 9/xht xhtml ml"> "> <title>CEO, Widget Inc.</title> <email>john.doe@widget.inc</email> <xsl:t <xsl:tem empl plate ate ma matc tch=" h="b: b:ca card" rd"> <phone>(202) 555-1414</phone> <html> <head> <logo uri="widget.gif"/> <title><xs <xsl: l:val value ue-o -of s f sele elect ct="b ="b:n :nam ame/t e/text ext() ()"/> "/></title> </card> </head> <body bgcolor="#ffffff"> <table border="3"> <tr> <td> <xsl xsl:a :appl pply- y-te templ mplate ates sel s selec ect= t="b: "b:nam name" e"/> /><br/> <xsl xsl:a :appl pply- y-te templ mplate ates sel s selec ect= t="b: "b:tit title le"/> "/><p/> <tt><xsl: <xsl:ap appl ply-t y-temp empla lates tes s select ect="b ="b:e :emai mail" l"/> /></ </tt><br/> An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6 XSLT for Business Cards (2/2) XSLT Stylesheets XSLT for Business Cards (2/2) XSLT Stylesheets <xsl xsl:i :if t f test="b: "b:pho phone ne"> "> Phone: <xsl <xsl:ap :apply ply-t -temp empla late tes s s sele elect ct="b ="b:p :pho hone" ne"/> /><br/> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" </xs /xsl: l:if> if> version="2.0"> </td> ... <td> </xsl:stylesheet> <xsl xsl:i :if t f test="b: "b:log logo" o"> <img src="{b:logo {b:logo/@ /@uri uri}"/> </xs /xsl: l:if> if> </td> � An XSLT stylesheet contains template rules </tr> </table> � The processor finds the most specific rule for the </body> </html> document root </xsl: </xsl:te temp mplat late> e> � It then executes the template body <xsl:t <xsl:tem empl plate ate ma matc tch=" h="b: b:na name| me|b:t b:tit itle| le|b: b:em email ail|b: |b:ph phone one"> "> <xsl xsl:v :val alue- ue-of se of selec lect= t="t "text ext()" ()"/> /> </xsl: sl:te temp mplat late> e> </xsl </x sl:st :styl yles eshee heet> t> An Introduction to XML and Web Technologies 7 An Introduction to XML and Web Technologies 8 2
Use of XPath in XSLT The XSLT Context Use of XPath in XSLT The XSLT Context � Specifying patterns for template rules � A context item (a node in the source tree or an atomic value) � Selecting nodes for processing � A context position and size � Computing boolean conditions � A set of variable bindings (mapping variable � Generating text contents for the output document names to values) � A function library (including those from XPath) � A set of namespace declarations An Introduction to XML and Web Technologies 9 An Introduction to XML and Web Technologies 10 The Initial Context Template Rules The Initial Context Template Rules � The context item is the document root <xsl:template match="..."> ... � The context position and size both have value 1 </xsl:template> � The set of variable bindings contains only global parameters � Find the template rules that match the contex node � The function library is the default one � Select the most specific one � The namespace declarations are those defined in � Evaluate the body (a sequence constructor ) the root element of the stylesheet An Introduction to XML and Web Technologies 11 An Introduction to XML and Web Technologies 12 3
Patterns and Matching Names, Modes, Priorities Patterns and Matching Names, Modes, Priorities � A pattern is a restricted XPath expression � Templates may have other attributes • it is a union of path expressions • each path expression contains a number of steps � name : used to call templates like function separated by / or // � mode : used to restrict the candidate templates • each step may only use the child or attribute axis � priority : used to determine specificity � A pattern matches a node if • starting from some node in the tree: • the given node is contained in the resulting sequence rcp:recipe/rcp:ingredient//rcp:preparation An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14 Sequence Constructors Literal Constructors Sequence Constructors Literal Constructors � Element and attribute constructors <xsl:stylesheet version="2.0" � Text constructors xmlns:xsl="http://www.w3.org/1999/XSL/Transform" � Copying nodes xmlns="http://www.w3.org/1999/xhtml"> � Recursive application <xsl:template match="/"> <html> � Repetitions <head> � Conditionals <title>Hello World</title> � Template invocation </head> <body bgcolor="green"> � Variables and parameters <b>Hello World</b> � Built-in template rules </body> </html> � Grouping </xsl:template> � Sorting </xsl:stylesheet> � Numbering An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16 4
Recommend
More recommend