Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath
2
Object Oriented Programming III2 Processing XML using XSLT XSLT is available on a number of platforms. Next week – how C# interacts with XSLT
3
Object Oriented Programming III3 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input
4
Object Oriented Programming III4 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Processing
5
Object Oriented Programming III5 The Catcher in the Rye J. D. Salinger Little, Brown and Company Output
6
Object Oriented Programming III6 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input
7
Object Oriented Programming III7 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> The default rules matches the root, library and block elements.
8
Object Oriented Programming III8 The Catcher in the Rye J. D. Salinger Little, Brown and Company The output is the same.
9
Object Oriented Programming III9 The Catcher in the Rye J. D. Salinger Little, Brown and Company Cliff Notes on The Catcher in the Rye Two books in the input
10
Object Oriented Programming III10 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> What’s the output?
11
Object Oriented Programming III11 The Catcher in the Rye J. D. Salinger Little, Brown and Company Cliff Notes on The Catcher in the Rye Illegal HTML
12
Object Oriented Programming III12 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input
13
Object Oriented Programming III13 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- --> We are not matching on publisher.
14
Object Oriented Programming III14 The Catcher in the Rye J. D. Salinger Little, Brown and Company We get the default rule matching the publisher and then printing its child.
15
Object Oriented Programming III15 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input
16
Object Oriented Programming III16 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> We can skip the publisher by matching and stopping the recursion.
17
Object Oriented Programming III17 The Catcher in the Rye J. D. Salinger
18
Object Oriented Programming III18 The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company A shelf has many books.
19
Object Oriented Programming III19 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Will this do the job?
20
Object Oriented Programming III20 The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company This is not what we want.
21
Object Oriented Programming III21 The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company Same input.
22
Object Oriented Programming III22 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Found a shelf Checks for a shelf and quits.
23
Object Oriented Programming III23 Found a shelf Output
24
Object Oriented Programming III24 The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company The Catcher in the Rye J. D. Salinger Little, Brown and Company Same input.
25
Object Oriented Programming III25 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> These are a few of my favorite books Produce a table of books.
26
Object Oriented Programming III26 These are a few of my favorite books 1 The Catcher in the Rye J. D. Salinger Little, Brown and Company 2 The XSLT Programmer's Reference Michael Kay Wrox Press 3 Computer Organization and Design Patterson and Henessey Morgan Kaufmann
27
Object Oriented Programming III27
28
Object Oriented Programming III28 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
29
Object Oriented Programming III29 XPATH First, we’ll look at three commonly used XSLT instructions: xsl:value-of xsl:template xsl:apply-templates
30
Object Oriented Programming III30 XPATH 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.
31
Object Oriented Programming III31 XPATH 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
32
Object Oriented Programming III32 XPATH 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
33
Object Oriented Programming III33 XPATH 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.
34
Object Oriented Programming III34 The Tree Structure of an XML Document Alan Turing computer scientist mathematician cryptographer See Harold Pg. 147
35
Object Oriented Programming III35 Richard M Feynman physicist Playing the bongoes Unicode ‘M’
36
Object Oriented Programming III36 / person born = “1914” died = “1952” id=“p342” person name first_name Alan <!– Did the word “computer scientist” exist in Turing’s day?”-- > profession
37
Object Oriented Programming III37 The root Element Nodes Text Nodes Attribute Nodes Comment Nodes Processing Instructions Namespace Nodes Nodes seen by XPath Constructs not seen by XPath CDATA sections Entity references Document Type Declarations
38
Object Oriented Programming III38 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" > :
39
Object Oriented Programming III39 Location Paths The root matched the root matched the root
40
Object Oriented Programming III40 Location Paths Child element location paths (relative to context node) computer scientist
41
Object Oriented Programming III41 Location Paths Attribute location paths (relative to context node) 1912
42
Object Oriented Programming III42 Location Paths Attribute location paths (relative to context node) 1912 1918
43
Object Oriented Programming III43 Location Paths Comment Location Step (comments don’t have names) Did the word "computer scientist" exist in Turing's day?
44
Object Oriented Programming III44 Location Paths Comment Location Step comment deleted Document content with comments replaced as shown. Default – no comments output
45
Object Oriented Programming III45 Location Paths Text Location Step (Text nodes don’t have names) computer scientist
46
Object Oriented Programming III46 Location Paths Processing Instruction Location Step type="text/xsl" href = "pi.xsl"
47
Object Oriented Programming III47 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.
48
Object Oriented Programming III48 Location Paths Matching with * Matches all elements and requests calls on sub-elements only. Nothing is displayed. The text nodes are never reached.
49
Object Oriented Programming III49 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.
50
Object Oriented Programming III50 Matching with Node What is the output?
51
Object Oriented Programming III51 Matching with Node -Output
52
Object Oriented Programming III52 Location Paths Matching with @* The @* wild card matches all attribute nodes.
53
Object Oriented Programming III53 Matching with @* Found an attribute What is the output?
54
Object Oriented Programming III54 Matching with @* - Output Found an attribute 1912 Found an attribute 1954 Found an attribute p342 Found an attribute 1918 Found an attribute 1988 Found an attribute p4567
55
Object Oriented Programming III55 Matching with @* 19121954p342 19181988p4567
56
Object Oriented Programming III56 Location Paths Multiple matches with | Matches all the elements. Skips the text nodes unless they describe a profession or hobby.
57
Object Oriented Programming III57 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.
58
Object Oriented Programming III58 Location Paths Selecting from all descendants with // TuringFeynman
59
Object Oriented Programming III59 Location Paths Selecting from all descendants with // Alan
60
Object Oriented Programming III60 Location Paths Selecting from all descendants with // AlanRichard
61
Object Oriented Programming III61 Location Paths Selecting from all descendants with // Richard
62
Object Oriented Programming III62 Specifying the Child Axis Consider the following path: /Envelope/Header/Signature The above is an abbreviation for /child::Envelope/child::Header/child::Signature
63
Object Oriented Programming III63 Using an Axis
64
Object Oriented Programming III64 What is the output?
65
Object Oriented Programming III65 Richard M Feynman Alan Turing Axis Example - Output
66
Object Oriented Programming III66 Writing Output to an Attribute
67
Object Oriented Programming III67 Writing Output to an Attribute
68
Object Oriented Programming III68 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.
69
Object Oriented Programming III69 Predicates Richard M Feynman
70
Object Oriented Programming III70 Predicates Richard M Feynman physicist Playing the bongoes
71
Object Oriented Programming III71 Predicates Alan Turing computer scientist mathematician cryptographer
72
Object Oriented Programming III72 Predicates Richard M Feynman physicist Playing the bongoes
73
Object Oriented Programming III73 Predicates <xsl:apply-templates select = "/people/person[@born < 1950]/ name[first_name='Alan']" /> Alan Turing
74
Object Oriented Programming III74 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.
75
Object Oriented Programming III75 General XPath Expressions 191.2191.8
76
Object Oriented Programming III76 General XPath Expressions Xpath Functions Person Person 1 Person 2
77
Object Oriented Programming III77 General XPath Expressions Xpath Functions Mr. T. Mr. T. Alan Turing Node set converted to string
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.