Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)

Similar presentations


Presentation on theme: "Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)"— Presentation transcript:

1 Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)

2 Processing XML using XSLT To use James Clark’s xt program visit his site at http://www.jclark.com/http://www.jclark.com/ and click on XML. The following programs were tested with the command line C:>xt somefile.xml somefile.xsl resultfile.html The xt classes (and xslt processing) may also be accessed via a servlet.

3 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input

4 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Processing

5 The Catcher in the Rye J. D. Salinger Little, Brown and Company Output

6 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input

7 <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 The Catcher in the Rye J. D. Salinger Little, Brown and Company The output is the same.

9 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 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> What’s the output?

11 The Catcher in the Rye J. D. Salinger Little, Brown and Company Cliff Notes on The Catcher in the Rye Illegal HTML

12 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input

13 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- --> We are not matching on publisher.

14 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 The Catcher in the Rye J. D. Salinger Little, Brown and Company Input

16 <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 The Catcher in the Rye J. D. Salinger

18 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 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Will this do the job?

20 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 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 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Found a shelf Checks for a shelf and quits.

23 Found a shelf Output

24 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 <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 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

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

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

30 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 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 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 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 The Tree Structure of an XML Document Alan Turing computer scientist mathematician cryptographer

35 Richard M Feynman physicist Playing the bongoes

36 / person born = “1914” died = “1952” id=“p342” person name first_name Alan <!– Did the word “computer scientist” exist in Turing’s day?”-- > profession

37 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 Location Paths The root <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > matched the root matched the root

39 Location Paths Child element location paths (relative to context node) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > computer scientist

40 Location Paths Attribute location paths (relative to context node) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > 1912

41 Location Paths Attribute location paths (relative to context node) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > 1912 1918

42 Location Paths Comment Location Step (comments don’t have names) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > Did the word "computer scientist" exist in Turing's day?

43 Location Paths Comment Location Step <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > comment deleted Document content with comments replaced as shown. Default – no comments output

44 Location Paths Text Location Step (Text nodes don’t have names) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > computer scientist

45 Location Paths Processing Instruction Location Step <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > type="text/xsl" href = "pi.xsl"

46 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.

47 Location Paths Matching with * <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > Matches all elements and requests calls on sub-elements only. Nothing is displayed. The text nodes are never reached.

48 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. Not implemented in XT

49 Location Paths Matching with @* The @* wild card matches all attribute nodes. XT does not like it in an but likes it in an

50 Location Paths Matching with @* <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > 19121954p342 19181988p4567

51 Location Paths Multiple matches with | Matches all the elements. Skips the text nodes unless they describe a profession or hobby.

52 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.

53 Location Paths Selecting from all descendants with // TuringFeynman

54 Location Paths Selecting from all descendants with // Alan

55 Location Paths Selecting from all descendants with // AlanRichard

56 Location Paths Selecting from all descendants with // Richard

57 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.

58 Predicates Richard M Feynman

59 Predicates Richard M Feynman physicist Playing the bongoes

60 Predicates Alan Turing computer scientist mathematician cryptographer

61 Predicates Richard M Feynman physicist Playing the bongoes

62 Predicates <xsl:apply-templates select = "/people/person[@born < 1950]/ name[first_name='Alan']" /> Alan Turing

63 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.

64 General XPath Expressions 191.2191.8

65 General XPath Expressions Xpath Functions Person Person 1 Person 2

66 General XPath Expressions Xpath Functions Mr. T. Mr. T. Alan Turing Node set converted to string

67 Escaping to Java Extension functions provide a mechanism for extending the capabilities of XSLT by escaping into another language Such as Java or JavaScript. If there is no namespace prefix on the function then it must be a core function built into XSLT. Otherwise, it’s an extension function.

68 General XPath Expressions Extended Xpath Functions <xsl:template name = "show-date" xmlns:Date = "http://www.jclark.com/xt/java/java.util.Date">

69 Escaping to Java Mon Mar 19 10:46:17 EST 2001

70 Escaping to Java // A simple bean saved under Www/beans/MyDate.java // The classpath c:\Jigsaw\Jigsaw\Jigsaw\Www\beans import java.util.*; public class MyDate { Date d; public MyDate() { d = new Date(); } public Date getDate() { return d; }

71 public String toString() { return "The date is " + d.toString(); } public static void main(String a[]) { MyDate x = new MyDate(); System.out.println(x); }

72 Escaping to Java <xsl:template name = "show-date" xmlns:Date = "http://www.jclark.com/xt/java/MyDate"> The date is Mon Mar 19 11:17:24 EST 2001


Download ppt "Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)"

Similar presentations


Ads by Google