XPathXML Path Language - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

XPathXML Path Language

Description:

ancestor. ancestor. parent. self (context node) following. following ... Ancestor-or-self. Descendant-or-self. Document order. forward. reverse. 10. Node Test ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 28
Provided by: museIn
Category:

less

Transcript and Presenter's Notes

Title: XPathXML Path Language


1
XPath(XML Path Language)
2
??
  • Introduction
  • Basics
  • Location Paths
  • Expressions
  • Core Function Library
  • Data Model

3
Introduction
  • Language for addressing parts of an XML document,
    designed to be used by XSLT and XPointer
  • Building blocks for other W3C standards
  • W3C Recommendation 16 November 1999
  • Version 1.0
  • Features
  • Address parts of an XML document
  • A common syntax and semantics for functionality
  • XSLT, XPointer
  • Manipulation of strings, numbers, booleans
  • Models an XML document as a tree of nodes
  • Operate on the abstract, logical structure of an
    XML document
  • Fully supports XML Namespace

4
Basics
  • Node type for XML document tree
  • Root node
  • Element node
  • Attribute node
  • Namespace node
  • Processing-instruction node
  • Comment node
  • Text node

5
Basics
  • Expression
  • Primary syntactic construct in XPath
  • Four basic types
  • Node-set
  • An unordered collection of nodes without
    duplicates
  • Boolean
  • True of false
  • Number
  • Floating-point number
  • String
  • a sequence of UCS characters

6
Basics
  • Context consists of
  • a node (context node)
  • a pair of non-zero positive integers (context
    position and context size)
  • a set of variable bindings
  • mappings from variable names to variable values
  • a function library
  • mappings from function names to functions
  • the set of namespace declarations in scope for
    the expression

7
Location Path
  • Most important expression in XPath
  • Selects a set of nodes relative to the context
    node
  • consists of location steps
  • location step axis node test optional
    predicate
  • Provide syntactic abbreviations
  • / document root
  • document element? parent

8
Location Path
  • Relative location path
  • one or more location steps separated by /
  • location steps are composed together from left to
    right
  • each node in the resulting set of one step
  • used as a context node for the following step
  • Absolute location path
  • / a relative location path (optional)
  • Location Step
  • Axis Node test Predicates
  • ex) child NAME position()5

9
Location Step
  • Axes
  • specifies the tree relationship between the nodes
    selected by location step and the context node

Ancestor-or-self
Document order
ancestor
ancestor
preceding
following
reverse
parent
following
preceding
self (context node)
following-sibling
Preceding-sibling
forward
child
child
descendant
descendant
Descendant-or-self
10
Node Test
  • Specifies the node type and expanded-name of the
    node
  • Every axis has principal node type
  • Attribute axis attribute
  • Namespace axis namespace
  • Other axes element
  • Node Test
  • true for any node of the principal node type
  • text() true for any text node
  • comment() true for any comment node
  • processing-instruction() true for any processing
    instruction
  • node() true for any node of any type
  • Name true for nodes which has the Name

11
Predicates
  • Filters a node-set w.r.t. an axis to produce a
    new node-set
  • Expr
  • Expr is evaluated for each node to be filtered
  • if true, the node is included in the new node-set
  • para3 is equivalent to paraposition()3

12
Location Path Examples
  • Examples
  • /
  • selects the document root node
  • /descendantpara
  • selects all the para elements in the same
    document
  • childparaposition()1
  • selects the first para child of the context node
  • preceding-siblingchapterposition()1
  • selects the previous chapter sibling of the
    context node
  • childparaattributetypewarningposition()
    5
  • selects the 5th para child of the context node
    that has a type attribute with value warning

13
Abbreviated Syntax
  • Location Step
  • child can be omitted from a location step
  • Default axis
  • Ex) childdiv/childpara ? div/para
  • attribute ? _at_
  • Ex) childparaattributetypewarning ?
    para_at_typewarning
  • /descendant-or-selfnode()/ ? //
  • Ex) /descendant-or-selfnode()/childpara ?
    //para
  • selfnode() ? .
  • Ex) selfnode()/descendant-or-selfnode()/child
    para ? .//para
  • parentnode() ? ..
  • Ex) parentnode()/childtitle ? ../title
  • //para1 vs /descendantpara1

14
Other Expressions
  • Function calls
  • FunctionName(Argument1, Argument2, )
  • Node-sets
  • Location path
  • operator
  • Computes the union of its operands(node-sets)
  • Booleans
  • or, and, , !, lt, lt, gt
  • lt, lt must be quoted like lt lt in XML
    documents
  • Left associative
  • 3 gt 2 gt 1 false

15
Expressions
  • Numbers
  • Floating-point number
  • Double-precision 64-bit format
  • Nan(Not-a-Number) value
  • Positive, negative infinity
  • Positive, negative zero
  • , -, div, mod
  • - operation typically needs to be preceded by
    whitespace
  • foo-bar, foo - bar
  • Strings
  • A sequence of zero or more characters
  • A character in XPath
  • A single Unicode abstract character
  • May be a pair of 16-bit Unicode code values

16
Core Function Library
  • XPath implementations must always include
  • Node set functions
  • number last()
  • number position()
  • number count(node-set)
  • node-set id(object)
  • Ex) id(foo)
  • string local-name(node-set?)
  • string namespace-uri(node-set?)
  • string name(node-set?)

17
Core Function Library
  • String Functions
  • string string(object?)
  • string concat(string, string, string)
  • boolean starts-with(string, string)
  • boolean contains(string, string)
  • string substring-before(string, string)
  • string substring-after(string, string)
  • string substring (string, number, number?)
  • number string-length(string?)
  • string normalize-space(string?)
  • string translate(string, string, string)

18
Core Function Library
  • Boolean functions
  • boolean boolean(bobject)
  • boolean not(boolean)
  • boolean true()
  • boolean false()
  • boolean lang(string)

19
Core Function Library
  • Number functions
  • number number(object?)
  • number sum(node-set)
  • number floor(number)
  • number ceiling(number)
  • number round(number)

20
Data Model
  • How XPath models an XML document as a tree?
  • Node type
  • Root nodes
  • Element nodes
  • Text nodes
  • Attribute nodes
  • Namespace nodes
  • Processing instruction nodes
  • Comment nodes
  • string-value
  • expanded-name local part namespace URI

21
Data Model
  • Root node
  • A element node for the document
  • Processing instruction, comment nodes for
    children
  • string-value
  • Concatenation of the string-values of all text
    node descendants
  • No expanded-name

22
Data Model
  • Element node
  • Element, comment processing instruction, text
    node for children
  • Has expanded-name
  • string-value
  • Concatenation of the string-values of all text
    node descendants
  • Unique IDs
  • Attributes declared in DTD as type ID

23
Data Model
  • Attribute node
  • Each element node has an associated set of
    attribute nodes
  • The element is the parent of each of the
    attribute nodes
  • Is not a child of its parent element
  • test whether two nodes have the same value
  • Not whether they are the same node
  • A defaulted attribute is treated the same as a
    specified attribute
  • Has expanded-name
  • string-value
  • Normalized value

24
Data Model
  • Namespace node
  • Each element has an associated set of namespace
    nodes
  • For each distinct namespace prefix in scope for
    the element
  • One for the default namespace if in scope for the
    element
  • Is not a child of its parent element
  • Has expanded-name
  • Namespace URI null
  • string-value
  • Namespace URI

25
Data Model
  • Processing instruction node
  • For every processing instruction
  • string-value
  • Part of the processing instruction following the
    target
  • Has expanded-name
  • Local part processing instructions target
  • Namespace URI null
  • XML declaration is not a processing instruction

26
Data Model
  • Comment node
  • string-value
  • Content of the comment
  • No expanded-name
  • Text Nodes
  • Character data
  • Never have an immediately following/preceding
    sibling text node
  • string-value
  • Contents
  • No expanded-name

27
Reference
  • http//www.w3c.org/TR/xpath
Write a Comment
User Comments (0)
About PowerShow.com