Presentation is loading. Please wait.

Presentation is loading. Please wait.

5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.

Similar presentations


Presentation on theme: "5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath."— Presentation transcript:

1 5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath

2 5-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe XML Path Language (XPath) Construct a location path Use XPath expressions and functions Test XPath expressions

3 5-3 Copyright © 2004, Oracle. All rights reserved. What Is XML Path Language? XML Path Language (XPath): Is primarily used to address the nodes of an XML document modeled as a tree of nodes Is named after its use of a path notation for navigating through the hierarchical structure of an XML document Uses a compact, non-XML syntax to form expressions for use in Uniform Resource Identifier (URI) and XML attribute values Fully supports XML Namespaces Is designed to be used by XML applications, such as XSLT and XPointer

4 5-4 Copyright © 2004, Oracle. All rights reserved. The XPath Model Document root num="1" 10 Administration num="2" 1 2 4 5 3

5 5-5 Copyright © 2004, Oracle. All rights reserved. XPath Expressions An XPath expression: Is the primary construct in XPath Is evaluated to yield an object, whose type can be: –A node set –A Boolean –A number –A string Is evaluated within a context, which includes: –A node called the context node –The context position and the context size –A function library among others Known as a location path, is commonly used

6 5-6 Copyright © 2004, Oracle. All rights reserved. The Location Path Expression The location path expression is one of the following: An absolute location path A relative location path: –Is made up of one or more location steps that specify navigation directions to nodes in an XML document –Is relative to the starting point called the context node 10 Administration /departments/department/department_id

7 5-7 Copyright © 2004, Oracle. All rights reserved. Results of Location Path Expressions A location path evaluates to a result that is: Empty A single node A set of nodes Examples: The absolute root location path Child element location steps (examples show location paths relative to the document root) / Relative: departments Absolute: /departments Relative: departments/department Absolute: /departments/department...

8 5-8 Copyright © 2004, Oracle. All rights reserved. Location Steps in XPath Expressions Each location step: Forms the address of a specific node, or set of nodes, to be selected Comprises three parts: –An axis –A node test –Zero or more predicates Has an unabbreviated and abbreviated form Acts like a filter for a selected node or node-set Is separated by a forward slash ( / ) character to form a compound location path expression axis::node-test[predicates]

9 5-9 Copyright © 2004, Oracle. All rights reserved. XPath Axes ancestor parent/ancestor self child/descendant descendant following-sibling preceding-sibling attribute namespace Context node

10 5-10 Copyright © 2004, Oracle. All rights reserved. XPath Node Test Types An XPath node test type can be: A node-name, such as: –The element name –The attribute name, if prefixed with an @ symbol –A qualified name, with namespace prefix An asterisk ( * ) The text() type The processing-instruction() type The comment() type The node() type Separated by the vertical bar (|) character to form multiple matches

11 5-11 Copyright © 2004, Oracle. All rights reserved. Unabbreviated and Abbreviated Expressions AbbreviatedUnabbreviated (with axis name) departmentschild::departments employee/salarychild::employee/child::salary *child::*.self::node()..parent::node()../salary parent::node()/child::salary @idattribute::id department/@numchild::department/attribute::num text() child::text() //salary /descendant-or- self::node()/child::salary

12 5-12 Copyright © 2004, Oracle. All rights reserved. Notes Only Page

13 5-13 Copyright © 2004, Oracle. All rights reserved. Abbreviated XPath Expression: Examples Note: The document root is the context node. //last_name employees/employee/name/first_name/text() employees//salary employees/employee/@employee_id Result: King Kochhar Result: Steven Neena Result: 24000 18000 Result: employee_id="100" employee_id="101" comment() Result:

14 5-14 Copyright © 2004, Oracle. All rights reserved. XPath Predicates An XPath predicate: Is a Boolean expression in square brackets evaluated for each node in the node-set With a numeric result is converted to a Boolean using the position() function May be provided with each location step May be combined with logical operators //department[department_name="Administration"] /departments/department[2] /departments/department[position()=2] //department[@num<3]/department_id[.="10"] //department[@num>2 and @num<=4]/department_name

15 5-15 Copyright © 2004, Oracle. All rights reserved. Notes Only Page

16 5-16 Copyright © 2004, Oracle. All rights reserved. Operators in XPath Expressions Comparison operators: – =, > –=, != Logical operators: –not, and, or Mathematical operators: –* –div, mod –+, - Literal strings are quoted with single or double quotes. Note: Operators are listed in the order of precedence.

17 5-17 Copyright © 2004, Oracle. All rights reserved. XPath Functions XPath functions: Are used in predicates or expressions Have a name followed by parenthesis, and zero or more arguments. For example: May return one of the following four types: –Boolean –Number –Node-set –String function-name(arguments,...) position()

18 5-18 Copyright © 2004, Oracle. All rights reserved. Boolean Functions Example: 's without a FunctionDescription boolean(o) Converts to a Boolean not(b) Returns true if its argument is false and vice versa true() Returns true false() Returns false /departments/department[boolean(not(manager_id))] Partial Results: 120 Treasury 1700...

19 5-19 Copyright © 2004, Oracle. All rights reserved. Number Functions FunctionDescription number(o) Converts the argument to a number sum(ns) Returns the sum for each node in a node-set ceiling(n) Returns the smallest integer that is greater than a given number floor(n) Returns the largest integer which is less than a given number round(n) Returns the closest integer to a number

20 5-20 Copyright © 2004, Oracle. All rights reserved. Node-Set Functions Examples: FunctionDescription last() Returns the context size position() Returns the context position id(o) Returns elements by their unique ID count(ns) Returns the number of nodes in a node-set local-name(ns) Returns the local node name (unqualified) //department[position()=1] //department[last()]/department_name Results: 10... Results: Payroll

21 5-21 Copyright © 2004, Oracle. All rights reserved. String Functions Example: FunctionDescription string(o) Converts an object to a string concat(s,s,...) Concatenates its arguments substring(s,n,n) Returns a substring of a string argument from a start position to a length contains(s,s) Accepts two arguments and returns true if the first argument contains the second starts-with(s,s) Takes two arguments and returns true if the first argument starts with the second; otherwise, it returns false string-length(s) Returns the length of a string //department[starts-with(department_name,'A')]

22 5-22 Copyright © 2004, Oracle. All rights reserved. XSLT and XPath XSLT: Transforms XML into plain text, HTML, or XML Specifies transformation rules in elements with attributes that use XPath expressions <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

23 5-23 Copyright © 2004, Oracle. All rights reserved. Testing XPath Expressions To view the results of XPath expressions, use: An XSL stylesheet to transform an XML document that can be viewed in an XML-enabled browser The TestXPath tool provided for this course, via the Tools > TestXPath menu

24 5-24 Copyright © 2004, Oracle. All rights reserved. Using the TestXPath Tool 1 2 4 5 3

25 5-25 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Describe XPath and its document model Construct a location path with location steps Use axes, node-tests, and predicates in XPath expressions Use XPath functions in predicates Test XPath expressions by using XSLT or TestXPath

26 5-26 Copyright © 2004, Oracle. All rights reserved. Practice 5: Overview This practice covers the following topics: Testing XPath expressions by using TestXPath Specifying XPath expressions in XSLT stylesheet elements to output region information in HTML format

27 5-27 Copyright © 2004, Oracle. All rights reserved. Full Notes Page

28 5-28 Copyright © 2004, Oracle. All rights reserved. Full Notes Page


Download ppt "5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath."

Similar presentations


Ads by Google