Download presentation
Presentation is loading. Please wait.
1
Seminar on Service Oriented Architecture
The XSLT Processing Model SOA Seminar 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. SOA Seminar
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 /TestXSLT SOA Seminar
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 match = "author"> <H3><xsl:apply-templates/></H3> <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:stylesheet> Processing SOA Seminar
5
<H1>The Catcher in the Rye</H1>
<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 SOA Seminar
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 SOA Seminar
7
The default rules matches the root, library and block elements.
<xsl:stylesheet xmlns:xsl=" 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 match = "author"> <H3><xsl:apply-templates/></H3> <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:stylesheet> The default rules matches the root, library and block elements. SOA Seminar
8
<H1>The Catcher in the Rye</H1>
<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. SOA Seminar
9
XSLT Example (3) <?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. SOA Seminar
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 match = "author"> <H3><xsl:apply-templates/></H3> <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> </xsl:stylesheet> What’s the output? SOA Seminar
11
<H1>The Catcher in the Rye</H1>
<HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> <P> <I>Little, Brown and Company</I> </P> <BODY>Cliff Notes on The Catcher in the Rye</BODY> </HTML> </BODY> Illegal HTML SOA Seminar
12
XSLT Example (4) <?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 SOA Seminar
13
We are not matching on publisher.
<xsl:stylesheet xmlns:xsl=" 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 match = "author"> <H3><xsl:apply-templates/></H3> <!-- <xsl:template match = "publisher"> <P><I><xsl:apply-templates/></I></P> --> </xsl:stylesheet> We are not matching on publisher. SOA Seminar
14
<H1>The Catcher in the Rye</H1>
<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. SOA Seminar
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 SOA Seminar
16
We can skip the publisher by matching and stopping the recursion.
<xsl:stylesheet xmlns:xsl=" 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 match = "author"> <H3><xsl:apply-templates/></H3> <xsl:template match = "publisher"> <!-- Skip the publisher --> </xsl:stylesheet> We can skip the publisher by matching and stopping the recursion. SOA Seminar
17
<H1>The Catcher in the Rye</H1>
<HTML> <BODY> <H1>The Catcher in the Rye</H1> <H3>J. D. Salinger</H3> </BODY> </HTML> SOA Seminar
18
XSLT Example (6) A shelf has many books. <?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> </shelf> A shelf has many books. SOA Seminar
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 match = "author"> <H3><xsl:apply-templates/></H3> <xsl:template match = "publisher"> <i><xsl:apply-templates/></i> </xsl:stylesheet> Will this do the job? SOA Seminar
20
<HTML> This is not what we want. <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. SOA Seminar
21
XSLT Example (7) Same input. <?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> </shelf> Same input. SOA Seminar
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. SOA Seminar
23
<BODY>Found a shelf</BODY> </HTML>
Output SOA Seminar
24
XSLT Example (8) Same input. <?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> </shelf> Same input. SOA Seminar
25
Produce a table of books.
<xsl:stylesheet xmlns:xsl=" 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> </tr> <xsl:template match = "title | author | publisher"> <td><xsl:apply-templates/></td> </xsl:stylesheet> Produce a table of books. SOA Seminar
26
<b>These are a few of my favorite books</b>
<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> <td>2</td> <td>The XSLT Programmer's Reference</td> <td>Michael Kay</td> <td>Wrox Press</td> <td>3</td> <td>Computer Organization and Design</td> <td>Patterson and Henessey</td> <td>Morgan Kaufmann</td> </tr> </table> </BODY> </HTML> SOA Seminar
27
SOA Seminar
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 SOA Seminar
29
XPATH First, we’ll look at three commonly used XSLT instructions:
xsl:value-of xsl:template xsl:apply-templates SOA Seminar
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. SOA Seminar
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 SOA Seminar
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 SOA Seminar
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. SOA Seminar
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> SOA Seminar
35
<person born="1918" died = "1988" id="p4567"> <name>
<first_name>Richard</first_name> <middle_initial>M</middle_initial> <last_name>Feynman</last_name> </name> <profession>physicist</profession> <hobby>Playing the bongoes</hobby> </person> </people> Unicode ‘M’ SOA Seminar
36
The XML Infoset is the abstract data model. / born = “1914” person
<?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 SOA Seminar
37
Constructs not seen by XPath
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 SOA Seminar
38
Note The following appears in each example below so it
has been removed from the slides. <xsl:stylesheet xmlns:xsl=" version="1.0" > : </xsl:stylesheet> SOA Seminar
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> SOA Seminar
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 SOA Seminar
41
Location Paths Attribute location paths (relative to context node)
<xsl:template match="/"> <xsl:value-of select = /> </xsl:template> <?xml version="1.0" encoding="utf-8"?> 1912 SOA Seminar
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 = /> </date> <date>1912</date><date>1918</date> SOA Seminar
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? SOA Seminar
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 SOA Seminar
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 SOA Seminar
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" SOA Seminar
47
Location Paths There are three wild cards: *, node(), @*
The * matches any element node. It will not match attributes, text nodes, comments or processing instructions nodes. SOA Seminar
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. SOA Seminar
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. SOA Seminar
50
Matching with Node <xsl:template match="node()">
<xsl:apply-templates/> </xsl:template> What is the output? SOA Seminar
51
Matching with Node -Output
<?xml version="1.0" encoding="UTF-8"?> SOA Seminar
52
Location Paths Matching with @*
wild card matches all attribute nodes. SOA Seminar
53
Matching with @* <xsl:template match="@*"> Found an attribute
<xsl:value-of select="."/> </xsl:template> <xsl:template match="node()"> <xsl:apply-templates <xsl:apply-templates/> What is the output? SOA Seminar
54
Matching with @* - Output
<?xml version="1.0" encoding="UTF-8"?> Found an attribute 1912 1954 p342 1918 1988 p4567 SOA Seminar
55
Matching with @* <xsl:template match = "person" >
<b> <xsl:apply-templates select = /> </b> </xsl:template> <?xml version="1.0" encoding="utf-8"?> <b> p342</b> <b> p4567</b> SOA Seminar
56
Location Paths Multiple matches with | Matches all the elements.
<xsl:template match = "profession|hobby" > <activity> <xsl:value-of select = "text()"/> </activity> </xsl:template> <xsl:template match = "*" > <xsl:apply-templates /> <xsl:template match = "text()" > Matches all the elements. Skips the text nodes unless they describe a profession or hobby. SOA Seminar
57
Location Paths Selecting from all descendants with //
// selects from all descendants of the context node as well as the context node itself. At the beginning of an Xpath expression, it selects from all descendants of the root node. SOA Seminar
58
Location Paths Selecting from all descendants with //
<xsl:template match = "//name/last_name/text()" > <xsl:value-of select = "." /> </xsl:template> <xsl:template match = "text()" > <?xml version="1.0" encoding="utf-8"?> TuringFeynman SOA Seminar
59
Location Paths Selecting from all descendants with //
<xsl:template match = "/" > <xsl:value-of select = "//first_name/text()" /> </xsl:template> <?xml version="1.0" encoding="utf-8"?> Alan SOA Seminar
60
Location Paths Selecting from all descendants with //
<xsl:template match = "/" > <xsl:apply-templates select = "//first_name/text()" /> </xsl:template> <xsl:template match = "text()" > <xsl:value-of select = "." /> <?xml version="1.0" encoding="utf-8"?> AlanRichard SOA Seminar
61
Location Paths Selecting from all descendants with //
<xsl:template match = "/" > <xsl:apply-templates select = "//middle_initial/../first_name" /> </xsl:template> <xsl:template match = "text()" > <xsl:value-of select = "." /> </xsl:stylesheet> <?xml version="1.0" encoding="utf-8"?> Richard SOA Seminar
62
Specifying the Child Axis
Consider the following path: /Envelope/Header/Signature The above is an abbreviation for /child::Envelope/child::Header/child::Signature SOA Seminar
63
Using an Axis <xsl:template match="people">
<xsl:apply-templates select="person"/> </xsl:template> <xsl:template match = "person" > <xsl:if test="position() = last()"> <xsl:value-of select="preceding-sibling::person/name"/> </xsl:if> SOA Seminar
64
<xsl:if test="position() != last()">
<xsl:value-of select="following-sibling::person/name"/> </xsl:if> </xsl:template> What is the output? SOA Seminar
65
Axis Example - Output <?xml version="1.0" encoding="UTF-8"?>
Richard M Feynman Alan Turing SOA Seminar
66
Writing Output to an Attribute
<xsl:template <someTag id="{.}"></someTag> </xsl:template> <xsl:template match="node()"> <xsl:apply-templates <xsl:apply-templates/> SOA Seminar
67
Writing Output to an Attribute
<?xml version="1.0" encoding="UTF-8"?> <someTag id="1912"/> <someTag id="1954"/> <someTag id="p342"/> <someTag id="1918"/> <someTag id="1988"/> <someTag id="p4567"/> SOA Seminar
68
Predicates In general, an Xpath expression may refer to more
than one node. Predicates allow us to reduce the number of nodes we are interested in. Each step in a location path may have a predicate that selects from the node list that is current at that step in the expression. The boolean expression in the predicate is tested against each node in the context node list. If the expression is false then that node is deleted from the list. SOA Seminar
69
Predicates <xsl:template match = "/" >
<xsl:apply-templates select = "//profession[.='physicist']/../name" /> </xsl:template> <xsl:template match = "text()" > <xsl:value-of select = "." /> <?xml version="1.0" encoding="utf-8"?> Richard M Feynman SOA Seminar
70
Predicates <xsl:template match = "/" >
<xsl:apply-templates select = /> </xsl:template> <xsl:template match = "text()" > <xsl:value-of select = "." /> <?xml version="1.0" encoding="utf-8"?> Richard M Feynman physicist Playing the bongoes SOA Seminar
71
Predicates <xsl:template match = "/" >
<xsl:apply-templates select = <= 1915]" /> </xsl:template> <xsl:template match = "text()" > <xsl:value-of select = "." /> <?xml version="1.0" encoding="utf-8"?> Alan Turing computer scientist mathematician cryptographer SOA Seminar
72
Predicates <xsl:template match = "/" >
<xsl:apply-templates select = <= 1919 >= 1917]" /> </xsl:template> <xsl:template match = "text()" > <xsl:value-of select = "." /> <?xml version="1.0" encoding="utf-8"?> Richard M Feynman physicist Playing the bongoes SOA Seminar
73
Predicates <xsl:template match = "/" >
<xsl:apply-templates select = < 1950]/ name[first_name='Alan']" /> </xsl:template> <?xml version="1.0" encoding="utf-8"?> Alan Turing SOA Seminar
74
General XPath Expressions
Xpath expressions that are not node sets can’t be used in the match attribute of an xsl:template element. They can be used for the values for the select attribute of xsl:value-of elements and in location path predicates. SOA Seminar
75
General XPath Expressions
<xsl:template match = "/" > <xsl:apply-templates select = "/people/person" /> </xsl:template> <xsl:template match = "person"> <xsl:value-of div 10" /> <xsl:template match = "text()"> <?xml version="1.0" encoding="utf-8"?> SOA Seminar
76
General XPath Expressions Xpath Functions
<xsl:template match = "/" > <xsl:apply-templates select = "/people/person" /> </xsl:template> <xsl:template match = "person"> Person <xsl:value-of select="position()" /> <xsl:template match = "text()"> <?xml version="1.0" encoding="utf-8"?> Person 1 Person 2 SOA Seminar
77
General XPath Expressions Xpath Functions
<xsl:template match = "/" > <xsl:apply-templates select = "//name[starts-with(last_name,'T')]"/> </xsl:template> <xsl:template match = "name"> Mr. T. <xsl:value-of select="." /> Node set converted to string <?xml version="1.0" encoding="utf-8"?> Mr. T. Alan Turing SOA Seminar
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.