1 / 77

95-733 Internet Technologies

95-733 Internet Technologies. Lecture 4: Programming with XSLT. 1. How do we execute XSLT?. Using a standalone tool such as Xalan. By calling Xalan from within a program. By calling Xalan from within a servlet. XSLT Example (1). <?xml version="1.0" ?>

mercury
Download Presentation

95-733 Internet Technologies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 95-733 Internet Technologies Lecture 4: Programming with XSLT 95-733 Internet Technologies 1

  2. How do we execute XSLT? • Using a standalone tool such as Xalan. • By calling Xalan from within a program. • By calling Xalan from within a servlet. 95-733 Internet Technologies

  3. XSLT Example (1) <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Input Netbeans Project 95-733/TestXSLT 95-733 Internet Technologies

  4. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "book"> <HTML><BODY><xsl:apply-templates/></BODY></HTML> </xsl:template> <xsl:template match = "title"> <H1><xsl:apply-templates/></H1> </xsl:template> <xsl:template match = "author"> <H3><xsl:apply-templates/></H3> </xsl:template> <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:template> </xsl:stylesheet> Processing 95-733 Internet Technologies

  5. <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <P> <I>Little, Brown and Company</I> </P> </BODY> </HTML> Output 95-733 Internet Technologies

  6. XSLT Example (2) <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <library> <block> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </block> </library> Input 95-733 Internet Technologies

  7. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "book"> <HTML><BODY><xsl:apply-templates/></BODY></HTML> </xsl:template> <xsl:template match = "title"> <H1><xsl:apply-templates/></H1> </xsl:template> <xsl:template match = "author"> <H3><xsl:apply-templates/></H3> </xsl:template> <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:template> </xsl:stylesheet> The default rules matches the root, library and block elements. 95-733 Internet Technologies

  8. <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <P> <I>Little, Brown and Company</I> </P> </BODY> </HTML> The output is the same. 95-733 Internet Technologies

  9. <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> <book>Cliff Notes on The Catcher in the Rye</book> </book> XSLT Example (3) There are two book elements in the input. 95-733 Internet Technologies

  10. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "book"> <HTML><BODY><xsl:apply-templates/></BODY></HTML> </xsl:template> <xsl:template match = "title"> <H1><xsl:apply-templates/></H1> </xsl:template> <xsl:template match = "author"> <H3><xsl:apply-templates/></H3> </xsl:template> <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:template> </xsl:stylesheet> What’s the output? 95-733 Internet Technologies

  11. <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <P> <I>Little, Brown and Company</I> </P> <HTML> <BODY>Cliff Notes on The Catcher in the Rye</BODY> </HTML> </BODY> </HTML> Illegal HTML 95-733 Internet Technologies

  12. <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> XSLT Example (4) Input 95-733 Internet Technologies

  13. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "book"> <HTML><BODY><xsl:apply-templates/></BODY></HTML> </xsl:template> <xsl:template match = "title"> <H1><xsl:apply-templates/></H1> </xsl:template> <xsl:template match = "author"> <H3><xsl:apply-templates/></H3> </xsl:template> <!-- <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:template> --> </xsl:stylesheet> We are not matching on publisher. 95-733 Internet Technologies

  14. <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> Little, Brown and Company </BODY> </HTML> We get the default rule matching the publisher and then printing its child. 95-733 Internet Technologies

  15. XSLT Example (5) <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Input 95-733 Internet Technologies

  16. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "book"> <HTML><BODY><xsl:apply-templates/></BODY></HTML> </xsl:template> <xsl:template match = "title"> <H1><xsl:apply-templates/></H1> </xsl:template> <xsl:template match = "author"> <H3><xsl:apply-templates/></H3> </xsl:template> <xsl:template match = "publisher"> <!-- Skip the publisher --> </xsl:template> </xsl:stylesheet> We can skip the publisher by matching and stopping the recursion. 95-733 Internet Technologies

  17. <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> </BODY> </HTML> 95-733 Internet Technologies

  18. XSLT Example (6) <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <shelf> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </shelf> A shelf has many books. 95-733 Internet Technologies

  19. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "book"> <HTML><BODY><xsl:apply-templates/></BODY></HTML> </xsl:template> <xsl:template match = "title"> <H1><xsl:apply-templates/></H1> </xsl:template> <xsl:template match = "author"> <H3><xsl:apply-templates/></H3> </xsl:template> <xsl:template match = "publisher"> <i><xsl:apply-templates/></i> </xsl:template> </xsl:stylesheet> Will this do the job? 95-733 Internet Technologies

  20. <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <i>Little, Brown and Company</i> </BODY> </HTML> <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <i>Little, Brown and Company</i> </BODY> </HTML> <HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <i>Little, Brown and Company</i> </BODY> </HTML> This is not what we want. 95-733 Internet Technologies

  21. XSLT Example (7) <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <shelf> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </shelf> Same input. 95-733 Internet Technologies

  22. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "shelf"> <HTML><BODY>Found a shelf</BODY></HTML> </xsl:template> </xsl:stylesheet> Checks for a shelf and quits. 95-733 Internet Technologies

  23. <HTML> <BODY>Found a shelf</BODY> </HTML> Output 95-733 Internet Technologies

  24. XSLT Example (8) <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="demo1.xsl"?> <shelf> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </shelf> Same input. 95-733 Internet Technologies

  25. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match = "shelf"> <HTML> <BODY> <b>These are a few of my favorite books</b> <table width = "640“ border = “5”> <xsl:apply-templates/> </table> </BODY> </HTML> </xsl:template> <xsl:template match = "book"> <tr> <td> <xsl:number/> </td> <xsl:apply-templates/> </tr> </xsl:template> <xsl:template match = "title | author | publisher"> <td><xsl:apply-templates/></td> </xsl:template> </xsl:stylesheet> Produce a table of books. 95-733 Internet Technologies

  26. <HTML> <BODY> <b>These are a few of my favorite books</b> <table width="640“ border = “5”> <tr> <td>1</td> <td>The Catcher in the Rye</td> <td>J. D. Salinger</td> <td>Little, Brown and Company</td> </tr> <tr> <td>2</td> <td>The XSLT Programmer's Reference</td> <td>Michael Kay</td> <td>Wrox Press</td> </tr> <tr> <td>3</td> <td>Computer Organization and Design</td> <td>Patterson and Henessey</td> <td>Morgan Kaufmann</td> </tr> </table> </BODY> </HTML> 95-733 Internet Technologies

  27. 95-733 Internet Technologies

  28. XPATH • Non-xml language used to identify particular parts • of an xml document • Used by XSLT for matching and selecting particular • elements to be copied into the result tree. • Used by Xpointer to identify a particular point in or part • of an xml document that an Xlink links to. • Slides adapted from “XML in a Nutshell” by Harold 95-733 Internet Technologies

  29. XPATH First, we’ll look at three commonly used XSLT instructions: xsl:value-of xsl:template xsl:apply-templates 95-733 Internet Technologies

  30. XPATH <xsl:value-of select = “XPathExpression” /> The xsl:value-of element computes the string value of an Xpath expression and inserts it into the result tree. XPath allows us to select nodes in the tree and different node types produce different values. 95-733 Internet Technologies

  31. XPATH <xsl:value-of select = “XPathExpression” /> element => the text content of the element after all tags are stripped text => the text of the node attribute => the value of the attribute root => the value of the root processing-instruction => the processing instruction data (<?, ?>, and the target are not included comment => the text of the comment (no comment symbols) namespace => the namespace URI node set => the value of the first node in the set 95-733 Internet Technologies

  32. XPATH <xsl:template match = “pattern” /> The xsl:template top-level element is the key to all of xslt. The match attribute contains a pattern (location path) against which nodes are compared as they’re processed. If the pattern matches a node, then the contents are instantiated 95-733 Internet Technologies

  33. XPATH <xsl:apply-templates select = “XPath node set expression” /> Find and apply the highest priority template that matches the node set expression. If the select attribute is not present then all children of the context node are processed. 95-733 Internet Technologies

  34. An XML Document See Harol147 <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href = "pi.xsl" ?> <people> <person born="1912" died = "1954" id="p342"> <name> <first_name>Alan</first_name> <last_name>Turing</last_name> </name> <!-- Did the word "computer scientist" exist in Turing's day? --> <profession>computer scientist</profession> <profession>mathematician</profession> <profession>cryptographer</profession> </person> 95-733 Internet Technologies

  35. <person born="1918" died = "1988" id="p4567"> <name> <first_name>Richard</first_name> <middle_initial>&#x4D;</middle_initial> <last_name>Feynman</last_name> </name> <profession>physicist</profession> <hobby>Playing the bongoes</hobby> </person> </people> Unicode ‘M’ 95-733 Internet Technologies

  36. The XML Infoset is the abstract data model. / <?xml-stylesheet type="text/xsl" href = “some.xsl" ?> born = “1914” person person died = “1952” id=“p342” name profession <!– Did the word “computer scientist” exist in Turing’s day?”-- > first_name Alan 95-733 Internet Technologies

  37. Nodes seen by XPath Constructs not seen by XPath The root Element Nodes Text Nodes Attribute Nodes Comment Nodes Processing Instructions Namespace Nodes CDATA sections Entity references Document Type Declarations 95-733 Internet Technologies

  38. Note The following appears in each example below so it has been removed from the slides. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > : : </xsl:stylesheet> 95-733 Internet Technologies

  39. Location Paths • The root <xsl:template match="/"> <a>matched the root</a> </xsl:template> <?xml version="1.0" encoding="utf-8"?> <a>matched the root</a> 95-733 Internet Technologies

  40. Location Paths • Child element location paths (relative to context node) <xsl:template match="/"> <xsl:value-of select = "people/person/profession" /> </xsl:template> computer scientist 95-733 Internet Technologies

  41. Location Paths • Attribute location paths (relative to context node) <xsl:template match="/"> <xsl:value-of select = "people/person/@born" /> </xsl:template> <?xml version="1.0" encoding="utf-8"?> 1912 95-733 Internet Technologies

  42. Location Paths • Attribute location paths (relative to context node) <xsl:template match="/"> <xsl:apply-templates select = "people/person" /> </xsl:template> <xsl:template match = "person"> <date> <xsl:value-of select = "@born" /> </date> </xsl:template> <date>1912</date><date>1918</date> 95-733 Internet Technologies

  43. Location Paths • Comment Location Step (comments don’t have names) <xsl:template match="/"> <xsl:value-of select = "people/person/comment()" /> </xsl:template> <?xml version="1.0" encoding="utf-8"?> Did the word "computer scientist" exist in Turing's day? 95-733 Internet Technologies

  44. Location Paths • Comment Location Step <xsl:template match = "comment()" > <i>comment deleted</i> </xsl:template> Document content with comments replaced as shown. Default – no comments output 95-733 Internet Technologies

  45. Location Paths • Text Location Step (Text nodes don’t have names) <xsl:template match="/"> <xsl:value-of select = "people/person/profession/text()" /> </xsl:template> computer scientist 95-733 Internet Technologies

  46. Location Paths • Processing Instruction Location Step <xsl:template match="/"> <xsl:value-of select = "processing-instruction()" /> </xsl:template> <?xml version="1.0" encoding="utf-8"?> type="text/xsl" href = "pi.xsl" 95-733 Internet Technologies

  47. Location Paths • Wild cards • There are three wild cards: *, node(), @* • The * matches any element node. It will not match • attributes, text nodes, comments or processing • instructions nodes. 95-733 Internet Technologies

  48. Location Paths • Matching with * • <xsl:template match = "*" > • <xsl:apply-templates select ="*" /> • </xsl:template> Matches all elements and requests calls on sub-elements only. Nothing is displayed. The text nodes are never reached. 95-733 Internet Technologies

  49. Location Paths • Matching with node() The node() wild card matches all nodes: element nodes, text nodes, attribute nodes, processing instruction nodes, namespace nodes and comment nodes. 95-733 Internet Technologies

  50. Matching with Node <xsl:template match="node()"> <xsl:apply-templates/> </xsl:template> What is the output? 95-733 Internet Technologies

More Related