Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Before The Class What’re the roles of us in this subject? –A researcher –A developer –A problem solver –IT related technologies What’s really confused.

Similar presentations


Presentation on theme: "1 Before The Class What’re the roles of us in this subject? –A researcher –A developer –A problem solver –IT related technologies What’s really confused."— Presentation transcript:

1 1 Before The Class What’re the roles of us in this subject? –A researcher –A developer –A problem solver –IT related technologies What’s really confused you? –XML: a markup language to model specific data from a specific domain –The architecture of XML application –The process of utilising related technologies (XML-based) as a problem solver

2 2 XML Path Language (XPath) Address the nodes of XML doc. trees

3 3 Introduction Nodes Location Paths – Axes & Node Tests –Using Axes and Node Tests Syntax and Expression Operations and Data Types Examples Functions Summary

4 4 Introduction – Why XPath? During the development of eXtensible StyleSheet Language Transformation (XSLT), another member of the XML family, known as XPointer was defined. –XPointer takes the idea of anchor tags to a new level. Both XPointer and XSLT needed a way to point to various parts of a document. XSLT needs it to select the part of a document that would be transformed XPointer for linking two documents. –The solution was to provide a common syntax and semantics that both XSLT and XPointer could use.

5 5 Introduction – Why XPath? (cont.) Besides, XML provides a rich, flexible and efficient way to mark up data, –but XML does not provide a way to locate specific part of nodes within a given document. XML processing steps vs. XPath –An XML doc. would need to be parsed and then searched through element by element to reach specific parts (DOM, SAX) Trouble: for large document, inefficient and error prone –XPath: providing expression syntax for other XML technologies to locate specific parts Effectively and efficiently This new subset was called XPath

6 6 Introduction What is XPath? –The XML Path Language (XPath) is a set of syntax and semantics for referring to portions of XML documents –A language for finding information in an XML document XPath is a string-based (expression) instead of structure-based language (XML) –A syntax for defining parts of an XML document –Uses path expressions to navigate elements and attributes (nodes) in XML documents –XPath contains a library of standard functions (for accessing the nodes) –Intended to be used by other specifications such as XSL, XSL Transformations (XSLT) and the XML Pointer Language (XPointer) and others.

7 7 Introduction (cont.) The XPath specification is the foundation for a variety of specifications, –Including XSLT and linking/addressing specifications like XPointer. An understanding of XPath is fundamental to a lot of advancing XML technology usages. XPath core: Path Expressions –XPath uses path expressions to select nodes or node-sets in an XML document. –These path expressions look very much like the expressions you see when you work with a traditional computer file system. (XPath uses a notation with forward slashes (/) similar the UNIX shell ) –The basic idea is to recall: a tree is much like the structures of files and folders on a hard drive

8 8 Introduction (cont.) XPath Standard Functions –XPath includes over 100 built-in functions (refer to W3C XPath document) –There are functions for: string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more XPath is Used in XSLT –A major element in the XSLT standard. –XSLT uses XPath extensively for matching -- testing whether a node matches a given pattern. –XSLT specifies the context used by XPath. –You should understand XPath if you want to work with XSLT –Without XPath knowledge you will not be able to create XSLT documents (different documents).

9 9 Introduction (cont.) XPath is a W3C Standard –XPath became a W3C Recommendation 16. November 1999. –XPath was designed to be used by XSLT, XPointer and other XML parsing software XPath vs. XSLT, XPoint, XQuery, … XML document XPath XSLT XPoint XQuery ………… Variable documents the locations of document structures or data process the information finding the information using XPath Generate

10 10 XPath Version XPath 2.0 –XPath 2.0 is a superset of XPath 1.0 and currently a W3C Working Draft. –Two W3C working groups are working on version 2.0 of XPath: the XML Query Working Group and the XSL Working Group. –XPath 2.0 has more power and is more robust because it supports a broader set of data types. –XPath 2.0 values use XML Schema types rather than simple strings, numbers, or booleans. –XPath 2.0 is backward-compatible so that 1.0 expressions behave the same in 2.0, with exceptions listed in the specification.

11 11 XPath Nodes – use to model a document tree An XML document –Are treated as trees of nodes (Tree structure with nodes) –Each node represents part of an XML document Seven types of node –Root –Element –Attribute –Text –Comment –Processing instruction –Namespace Attributes and namespaces are not children of their parent node –They describe their parent nodes

12 12 XPath Nodes An XPath tree has a single root node Each XPath tree node has a string representation –Called a string-value, only text() nodes, not attribute and namespace nodes –Example: Web Programming II –? Example II: Web Text Book Document order: Nodes in an XPath tree have an ordering –Determined by the order appeared in the original XML doc. Expanded-name of certain nodes (refers to Fig 11.5) –Local part: tag name –Namespace URI: prefix

13 13 Fig. 11.5XPath node types

14 14 Fig. 11.5XPath node types. (Part 2)

15 15 XPath Nodes (cont.) Harry Potter J K. Rowling 2005 29.99 Example of nodes in the XML document above: (element node) J K. Rowling (element node) lang="en" (attribute node)

16 16 An Example of Presenting an XML Doc. In XPath <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://MyXPathExample.com AuctionItemList.xsd"> 1 5 56 Miles Smiles album, CD miles1965 0 70 2 New 256m MP3 player jimmy ………………………………

17 17 XPath's view of an XML doc.

18 Outline 18 Root node Comment nodes Element nodes Attribute nodes Text nodes 1 2 3 4 5 6 7 8 9 <![CDATA[ 10 11 // C++ comment 12 if ( this->getX() < 5 && value[ 0 ] != 3 ) 13 cerr displayError(); 14 ]]> 15 16 17 C++ How to Program by Deitel & Deitel 18 Comment nodesRoot node Element nodes Attribute nodes Text nodes Fig. 11.1Simple XML document.

19 19 Fig. 11.2XPath tree for Fig. 11.1. Attribute Title C++ How to Program Element book Comment Fig. 11.1 : simple.xml Comment Simple XML document Attribute edition 3 Root Text C++ How to Program by Deitel & Deitel Element sample Text // C++ comment if (this -> getX() displayError();

20 Outline 20 Fig. 11.3XML document with processing-instruction and namespace nodes. Root node Comment nodes Namespace nodes Processing instruction node Element nodes Text nodes Attribute nodes 1 2 3 4 5 6 7 8 9 Processing Instruction and Namespace Nodes 10 11 12 13 14 15 16 <deitel:book deitel:edition = "1" 17 xmlns:deitel = "http://www.deitel.com/xmlhtp1"> 18 XML How to Program 19 20 21 22 23 Comment nodesRoot node Element nodes Namespace nodes Text nodes Processing instruction node Attribute nodes

21 21 Continued on next slide... Fig. 11.4Tree diagram of an XML document with a processing-instruction node Root Comment Fig. 11.3 : simple2.xml Comment Processing instructions and namespaces Element html Namespace http://www.w3.org/TR/REC-html40 Element head Text Processing instructions and Namespcae Nodes Element title

22 22 Fig. 11.4Tree diagram of an XML document with a processing-instruction node. (Part 2) Continued from previous slide Processing Instruction deitelprocessor example = "fig11_03.xml" Element body Element book Attribute edition 1 Namespace http://www.deitel.com/xmlhtp1 Element title Text XML How to Program

23 23 Location Paths What we already know? –The structure of an XML document in XPath How can we use the structure to locate particular parts of a document? (Location path) –Just like we use to locate a specific file or folder in a file system? –The most useful and widely used feature of XPath –An expression specifying how to navigate XPath tree

24 24 Location Paths (cont.) A location path is composed of location steps Each location step composed of –Each location step has the following form: axis-name :: node-test [predicate]* –Axis: specifies the tree relationship between the nodes selected by the location step and the context node –Node test: specifies the node type and expanded-name of the nodes selected by the location step –Predicate: use arbitrary expressions to further refine the set of nodes selected by the location step

25 25 Location Paths (cont.) The location path sets the context of the node that you're trying to find. The context is set using the location path of the root (match="/"). To code a location path, you can use an abbreviated or non-abbreviated syntax. “abbreviated” is the most widely used; the unabbreviated syntax is also more complex You might need to check which one your parser supports. An example of the non-abbreviated syntax is shown below. (From the May 2000 release, the MSXML parser supports both.) –child::para selects the para element children of the context node

26 26 Location Paths (cont.) Two kinds of location path: relative location paths and absolute location paths A relative location path is a sequence of location steps separated by /. For example: list/item[ currentPrice < 20.0 ] –This location path consists of two location steps: the first, list, selects a set of nodes relative to the context node; the second, item[currentPrice < 20.0], selects a set of nodes in the subset identified by the first step; and so on, if there are more nodes.

27 27 Location Paths (cont.) An absolute location path consists of a /, optionally followed by a relative location path, with / referring to the root node. An absolute location path is basically a relative location path evaluated in the context of the root node, for example: /list/item[ currentPrice < 20.0 ] –With absolute location paths (location paths that start with /), the context node isn't meaningful because the path is always evaluated from the root node

28 28 Axes XPath searches are made relative to context node The context for a query is the node in the source XML document currently being processed –Context node: reference node where you start to locate specific parts in a document –Examp1: xsl:template match="/", we are in the context of the root of the XML document –Examp2: xsl:for-each loop, the context is whichever node we are currently looping through Axis –Indicates which nodes, relative to context node, are included in search –Dictates node ordering in set –Forward axes select nodes that follow context node –Reverse axes select nodes that precede context node

29 29 Fig. 11.6XPath axes. Summaries the 13 XPath Axes and their ordering and provides a description

30 30 Node Tests Node tests –A set of nodes selected by axis is refined with node tests –Rely upon axis’ principle node type For attribute axes, the principle node type is attribute For namespace axes, the principle node type is namespace All other axes, the principle node type is element –Corresponds to type of node axis can select

31 31 Fig. 11.7Some XPath node tests.

32 32 Location Paths Using Axes and Node Tests Location step –Axis and node test separated by double colon ( :: ) –Optional predicate enclosed in square brackets ( [] ) –Some examples: Select all element-node children of context node child::* Select all text-node children of context node child::text() Select all text-node grandchildren of context node child::*/child::text()

33 33 Fig. 11.8Some location-path abbreviations.

34 Outline 34 Fig. 11.9XML document that marks up book translations. 1 2 3 4 5 6 7 8 9 Java How to Program 10 Spanish 11 Chinese 12 Japanese 13 French 14 Japanese 15 16 17 18 C++ How to Program 19 Korean 20 French 21 Spanish 22 Italian 23 Japanese 24 25 26

35 35 Output for Fig. 11.9

36 36 Fig. 11.10 XPath tree for books.xml Other nodes… Continued on next slide… Element book Text Java How to Program Element title Attribute edition 1 Element translation Text Spanish Attribute edition 1 Element translation Text Chinese

37 37 Fig. 11.10 XPath tree for books.xml Continued from previous slide… Other nodes… Attribute edition 2 Element translation Text French Element translation Attribute edition 2 Text Japanese Element translation Attribute edition 1 Text Japanese

38 38 Location Paths Using Axes and Node Tests (cont.) Which books have Japanese translations? –Use root node of XPath tree as context node –Use predicate Boolean expression for filtering nodes from search Compare string value of current node to string ‘ Japanese ’ /books/book/translation[. = ‘Japanese’]/../title

39 39 XPath Syntax XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps The XML Example Document Harry Potter 29.99 Learning XML 39.95

40 40 XPath Syntax (cont.) Selecting Nodes –XPath uses path expressions to select nodes in an XML document. –The node is selected by following a path or steps. –The most useful path expressions are listed below ExpressionDescription elementSelects all child nodes of the node /Selects from the root node // Selects nodes in the document from the current node that matches the selection no matter where they are. Selects the current node.. Selects the parent of the current node @Selects attributes

41 41 XPath Syntax (cont.) Examples –In the table below we have listed some path expressions and the result of the expressions Path ExpressionResult bookstore Selects all the child nodes of the bookstore element /bookstore Selects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/book Selects all book elements that are children of bookstore //book Selects all book elements no matter where they are in the document bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element //@lang Selects all attributes that is named lang

42 42 XPath Syntax (cont.) Predicates –Predicates are used to find a specific node or a node that contains a specific value. –Predicates are always embedded in square brackets [ ] Examples –In the table below we have listed some path expressions with predicates and the result of the expressions Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element /bookstore/book[last()]Selects the last book element that is the child of the bookstore element //title[@lang]Selects all the title elements that have an attribute named lang //title[@lang='eng']Selects all the title elements that have an attribute named lang with a value of 'eng' /bookstore/book[price>35. 00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 /bookstore/book[price>35. 00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

43 43 XPath Syntax (cont.) Selecting Unknown Nodes XPath wildcards can be used to select unknown XML elements. WildcardDescription *Matches any element node @*Matches any attribute node node()Matches any node of any kind

44 44 XPath Syntax (cont.) Examples In the table below we have listed some path expressions and the result of the expressions: Path ExpressionResult /bookstore/* Selects all the child nodes of the bookstore element //*Selects all elements in the document //title[@*]Selects all title elements which have any attribute

45 45 XPath Syntax (cont.) Selecting Several Paths By using the | operator in an XPath expression you can select several paths. Examples In the table below we have listed some path expressions and the result of the expressions: Path ExpressionResult //book/title | //book/price Selects all the title AND price elements of all book elements //title | //price Selects all the title AND price elements in the document /bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

46 46 Predicates Predicates are used in location paths to filter the current set of nodes. A predicate contains a boolean expression (or an expression that can be easily converted to boolean) Each member of the current node-set is tested against the boolean expression and kept if the expression is true; otherwise, it is rejected. A predicate is enclosed in square brackets, [ ].

47 47 Predicates (cont.) Have a look at the following location path: List / item / currentPrice [ @currency = "EUR“ ] –During evaluation, all currentPrice elements in the XML doc. are in the selected node-set. –Then, the @currency="EUR" predicate is evaluated and the currentPrice elements whose currencies do not contain the EUR value are rejected. Predicates can also use the relational operators >, =, <=, and !=. They can also use Boolean operators,

48 48 XPath Operator XPath expressions returns either a node-set, a string, a Boolean, or a number A list of the operators that can be used in XPath expressions Oper.DescriptionExampleReturn value |Computes two node-sets //book | //cdReturns a node-set with all book and cd elements +Addition6 + 410 -Subtraction6 - 42 *Multiplication6 * 424 divDivision8 div 42 =Equalprice=9.80true if price is 9.80 false if price is 9.90 !=Not equalprice!=9.80true if price is 9.90 false if price is 9.80

49 49 XPath Operator (cont.) <Less thanprice<9.80true if price is 9.00 false if price is 9.80 <=Less than or equal to price<=9.80true if price is 9.00 false if price is 9.90 >Greater thanprice>9.80true if price is 9.90 false if price is 9.80 >=Greater than or equal to price>=9.80true if price is 9.90 false if price is 9.70 or price=9.80 or price=9.70 true if price is 9.80 false if price is 9.50 and price>9.00 and price<9.90 true if price is 9.80 false if price is 8.50 modModulus (division remainder) 5 mod 21

50 50

51 51 XPath Example Learn some basic XPath syntax by looking at some examples "books.xml": Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 ……………………………………………………………………………

52 52 XPath Example (cont.) applied XPath expression in XML DOM object 1. Selecting Nodes Use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document: set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("books.xml") xmlobject.selectNodes(path expression)

53 53 XPath Example (cont.) 1. Select all book Nodes The following example selects all the book nodes under the bookstore element: xmlDoc.selectNodes("/bookstore/book") 2. Select the First book Node The following example selects only the first book node under the bookstore element: xmlDoc.selectNodes("/bookstore/book[0]") Note: IE 5 and 6 has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]

54 54 XPath Example (cont.) 3. Select the prices The following example selects the text from all the price nodes: xmlDoc.selectNodes("/bookstore/book/price/text()") 4. Selecting price Nodes with Price>35 The following example selects all the price nodes with a price higher than 35: xmlDoc.selectNodes("/bookstore/book[price>35]/price") 5. Selecting title Nodes with Price>35 The following example selects all the title nodes with a price higher than 35: xmlDoc.selectNodes("/bookstore/book[price>35]/title")

55 55 Node-set Operators and Functions Node-set operators –Manipulate node sets to form others Node-set functions –Perform actions on node-sets returned by location paths

56 56 Fig. 11.11Node-set operators.

57 57 Fig. 11.12Some node-set functions. http://www.w3.org/TR/xpath-functions

58 58 Node-set Operators and Functions (cont.) Location-path expressions Combine node-set operators and functions Select all head and body children element nodes head | body Select last title element node in head element node head/title[ last() ] Select third book element book[ position() = 3 ] –Or alternatively book[ 3 ] Return total number of element-node children count( * ) Select all book element nodes in document //book

59 Outline 59 Fig. 11.13 List of companies with stock symbols. 1 2 3 4 5 6 7 8 9 Intel Corporation 10 11 12 13 Cisco Systems, Inc. 14 15 16 17 Dell Computer Corporation 18 19 20 21 Microsoft Corporation 22 23 24 25 Sun Microsystems, Inc. 26 27 28 29 CMGI, Inc. 30 31 32

60 Outline 60 Fig. 11.14 Demonstrating some String functions. XPath string functions 1 2 3 4 5 6<xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 8 9 10 11 12 13 14 15 16 <xsl:if test = 17 "starts-with(@symbol, 'C')"> 18 19 20 <xsl:value-of select = 21 "concat(@symbol,' - ', name)"/> 22 23 24 25 26 27 28 29 30 XPath string functions

61 Outline 61 Output for Fig. 11.14

62 62 That’s it for today! HomeWork # 3 –Text book P. 316, –Exercises 11.3 & 11.4


Download ppt "1 Before The Class What’re the roles of us in this subject? –A researcher –A developer –A problem solver –IT related technologies What’s really confused."

Similar presentations


Ads by Google