CIS336 Website design, implementation and management also Semester 2 of CIS219, CIS221 and IT226 - PowerPoint PPT Presentation

About This Presentation
Title:

CIS336 Website design, implementation and management also Semester 2 of CIS219, CIS221 and IT226

Description:

www.titanmusic.com/teaching/cis336-2006-7.html. 2. What is XPath? ... http://www.brics.dk/ixwt/recipes. is bound to the namespace prefix rcp ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 60
Provided by: davidme152
Category:

less

Transcript and Presenter's Notes

Title: CIS336 Website design, implementation and management also Semester 2 of CIS219, CIS221 and IT226


1
CIS336Website design, implementation and
management(also Semester 2 of CIS219, CIS221 and
IT226)
  • Lecture 3
  • XPath
  • (Based on Møller and Schwartzbach, 2006, Chapter
    3)

David Meredith d.meredith_at_gold.ac.uk www.titanmus
ic.com/teaching/cis336-2006-7.html
2
What is XPath?
  • XPath is a language for selecting parts of and
    navigating around an XML tree
  • Used in
  • XML Schema for uniqueness and scope descriptions
  • XSLT for pattern matching and selection
  • XQuery for selection and iteration
  • XLink and XPointer
  • XPath can also be used to do computations on data
    values
  • Example of an XPath expression//rcpingredient_at_
    amount'0.5' and _at_unit'cup'/_at_name
  • This expression uses abbreviations (// and _at_) and
    tacit conventions
  • XPath 1.0 was a relatively simple language
  • Through interaction with XQuery, it has become a
    much larger language called XPath 2.0
  • XPath 2.0 specification is a W3C proposed
    recommendation (November 2006), available here
    http//www.w3.org/TR/xpath20/

3
Location steps and paths
  • An XPath location path evaluates to a sequence of
    nodes in an XML tree
  • Sequence never contains duplicates of identical
    nodes
  • However it is possible for two or more nodes to
    contain the same values and therefore be equal
  • Location path is sequence of location steps,
    separated with / character, e.g.,
    childrcprecipeattributeid'117'/childrcp
    ingredient/attributeamount
  • This expression selects
  • all the amount attributes
  • in rcpingredient nodes that are children of
  • rcprecipe nodes with an attribute called id
    with value '117 that are
  • children of the context node (which is assumed
    here to be the root element)
  • Expression consists of three location steps, each
    with the format axisnodetestpred1pred2whe
    re axis is the axis, nodetest is a node test and
    pred1 and pred2 are predicates which are XPath
    expressions
  • Axis, nodetest and predicates are increasingly
    specific definitions of the sequence of nodes
    that the location step selects

4
Location step maps context onto new sequence of
nodes
  • Location step always evaluated relative to a
    context and always evaluates to a sequence of
    nodes
  • The context is itself a sequence of nodes
  • Therefore a location step transforms one sequence
    of XML tree nodes, called the context, into
    another sequence of XML tree nodes
  • The output sequence is generated by concatenating
    the result of replacing each node, x, in the
    input sequence with the result of evaluating the
    location step relative to x as the context node

5
An Example
A
B
B
C
C
D
F
E
E
C
F
F
F
E
E
F
F
6
An Example
A
B
B
C
C
D
F
E
E
C
F
F
F
E
E
F
F
Context node
7
An Example
A
B
B
C
C
D
F
E
E
C
F
F
F
E
E
F
F
descendantC
8
An Example
A
B
B
C
C
D
F
E
E
C
F
F
F
E
E
F
F
descendantC/childE
9
An Example
A
B
B
C
C
D
F
E
E
C
F
F
F
E
E
F
F
descendantC/childE/childF
10
Contexts
  • The context of an XPath evaluation consists of
  • a context node (a node in an XML tree)
  • a context position and size
  • If location path isdescendantC/childE/child
    Fand first two location steps have been
    evaluated, then we have the sequence (E1, E2, E3)
    shown at left
  • childF location step then evaluated on E1, E2
    and E3 in turn
  • In each case, context size is 3 because input
    sequence for this step contains 3 nodes (E1, E2,
    E3)
  • When childF evaluated on E1, context position
    is 1 when evaluated on E2, context position is
    2 and so on.
  • a set of variable bindings (mapping variable
    names to values)
  • a function library
  • XPath specification guarantees that context
    provides set of core functions
  • a set of namespace declarations
  • For example, in our examples, we assume that the
    namespacehttp//www.brics.dk/ixwt/recipesis
    bound to the namespace prefix rcp
  • The application determines the initial context
  • If the path starts with / then
  • the initial context node is the root node (not
    the root element)
  • the initial position and size are 1

A
B
B
C
C
D
F
E
E
C
1
F
F
F
E
E
F
2
3
F
11
Axes
  • child
  • Children of context node
  • NB excludes attribute nodes
  • descendant
  • Descendants of context node
  • NB excludes attribute nodes
  • parent
  • Unique parent node or empty sequence if context
    node is root node
  • ancestor
  • All ancestors of the context node, from parent to
    root node
  • following-sibling
  • Right-hand siblings of context node
  • Empty sequence if context node is an attribute
    node
  • preceding-sibling
  • Left-hand siblings of context node
  • Empty sequence if context node is an attribute
    node
  • following
  • All nodes appearing later in the document than
    the context node
  • Excludes descendants of context node
  • An axis is a sequence of nodes evaluated relative
    to the context node
  • First approximation to sequence of nodes we want
    to obtain as the result of a location step
  • XPath supports 12 different axes

12
Axis Directions
  • Each axis has a direction
  • Forwards means document order
  • child, descendant, following-sibling, following,
    self, descendant-or-self
  • Backwards means reverse document order
  • parent, ancestor, preceding-sibling, preceding,
    ancestor-or-self
  • Stable (i.e., always same output for same input)
    but depends on the implementation
  • attribute
  • self, ancestor, descendant, preceding and
    following together form a disjoint partition of
    all the nodes in an XML tree

13
The parent Axis
(4)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
14
The child Axis
(14,15)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
15
The descendant Axis
(14,15,16,17,18)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
16
The ancestor Axis
(4,2,1)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
17
The following-sibling Axis
(19)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
18
The preceding-sibling Axis
(9,5)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
19
The following Axis
(19,20,21,22,23,24,25,26,27,28)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
20
The preceding Axis
(12,11,10,9,8,7,6,5,3)
1
22
20
2
3
21
23
24
4
25
19
13
9
5
27
26
15
14
10
8
6
17
16
12
11
7
18
28
21
Node tests
  • Second part of a location step is the node test
    childrcprecipeattributeid'117'/childrcp
    ingredient/attributeamount
  • Types of node test
  • text() selects only the character data nodes
  • comment() selects only the comment nodes
  • processing-instruction() selects only the
    processing instruction nodes
  • node() selects all nodes
  • selects all nodes in the axis preceding the
    node test
  • If axis is not attribute, then only element nodes
    selected
  • name selects the nodes with the given QName, name
  • localname selects the nodes with given NCName,
    localname, in any namespace
  • prefix selects all nodes in the same way as ,
    but only in the specified namespace

22
Resolving names without namespaces
  • In XPath 1.0, missing namespace prefix
    interpreted as empty URI, "", not default
    namespace
  • Bug fixed in XPath 2.0, where empty prefix
    interpreted as default namespace, not empty URI
  • However, most tools implement XPath 1.0
  • Suppose we want to select the ref attribute of
    the subwidget element
  • Could try /childwidget/childbig/childsubwi
    dget/attributeref
  • But this wont work because each node name in
    expression is interpreted as being from the empty
    namespace
  • In fact, no XPath expression will work here!
  • In order to work XPath 1.0 and 2.0, every element
    name has to be explicitly qualified with
    namespace, as in lower example and XPath
    expression must be changed to /childwdgwidget/
    childwdgbig/childwdgsubwidget/attributeref

23
Predicates
  • Final part of location step consists of zero or
    more predicateschildrcprecipeattributeid'
    117'/childrcpingredient/attributeamount
  • Predicate is an XPath expression, evaluated as a
    boolean condition
  • XPath expressions are as rich as expressions in a
    general purpose programming language like Java
  • Can produce values of different types
  • e.g., numbers, booleans, strings, sequences
  • When used as a predicate, the value of an XPath
    expression is coerced into a boolean value
  • A number is coerced to true when it is equal to
    the current context position
  • A string is coerced to true when it has non-zero
    length
  • A sequence is coerced to true when it has
    non-zero length
  • Boolean conditions can be combined using the
    operators and and or and the function not
  • Variables from the context can be referenced
    using the syntax foo where foo is the variable
    name
  • The usual arithmetic (,-,,div) and comparison
    (, !, lt, lt, etc.) operators are also available
  • Sometimes useful to use location paths as
    predicates/descendantrcprecipedescendantrc
    pingredientattributenamesugarselects
    every recipe node that contains sugar
    whereas/descendantrcprecipe/descendantrcpin
    gredientattributenamesugarselects every
    ingredient node whose name is sugar.

24
More on predicates
  • The predicates in a location step are evaluated
    left-to-right
  • i.e., the first predicated is evaluated,
    producing a sequence of nodes which forms the
    context for the second predicate, and so on
  • This means that changing the order of the
    predicates can change the value of the result of
    the expression
  • For example/descendantrcpingredientposition(
    )3position()1returns every ingredient which
    is the third ingredient in a recipe
    whereas/descendantrcpingredientposition()1
    position()3 returns the empty sequence because
    once the first ingredient in each recipe has been
    selected, there is only one element in the
    context when the second predicated is evaluated
    (and therefore no third element)
  • If we combine two predicates with and, then this
    also generally gives a different
    result/descendantrcpingredientposition()3
    and position()1returns the empty sequence
    because there is no element whose position within
    the current context is both 3 and 1

25
Typical location paths
  • XPath language is large, but you usually only use
    a small part of it
  • There are a few patterns that are used
    particularly often
  • Most commonly used axes are child, descendant and
    attribute
  • , test() and QName are most commonly-used node
    tests

26
Some examples
  • /descendantrcprecipe/childrcptitle
  • Selects every title node in every recipe
  • /descendantrcprecipe/descendantrcpingredient
    /attributename
  • Selects the name of every ingredient in every
    recipe
  • /descendantrcp/childtext()
  • Selects every character data node in the
    collection
  • attributeamount
  • Selects the nodes in the context that contain an
    attribute called amount
  • attributeamount'0.5'
  • Selects nodes in the context that have an
    attribute whose name is amount and whose value is
    '0.5'
  • attributename!'flour'
  • Selects nodes in the context that do not have a
    name attribute whose value is 'flour
  • attributeamountlt0.5 and attributeunit'cup'
  • Selects context nodes that have an amount
    attribute with a value less than 0.5 and a unit
    attribute whose value is cup
  • position()2
  • Selects the second node in the context
  • descendantrcpingredient
  • Selects those nodes in the context that contain
    an ingredient node

27
Using XPath in other languages
  • XPath expressions often appear as attribute
    values in other XML languages (e.g., XML Schema
    and XSLT)
  • When used in other XML languages, all special
    characters have to be escaped, e.g.
  • ltxslapply-templates select"descendantrcpingre
    dientattributeamountlt0.5" /gt
  • Instead of
  • ltxslapply-templates select"descendantrcpingre
    dientattributeamountlt0.5" /gt

28
Abbreviations
  • XPath allows certain abbreviations that make
    expressions easier to write
  • If no axis is used, default axis of child is
    used /childrcpcollection/childrcprecipe/chi
    ldrcpingredientis equivalent
    to /rcpcollection/rcprecipe/rcpingredient
  • attribute axis can be replaced with
    _at_ /rcpcollection/rcprecipe/rcpingredient/attri
    buteamountis equivalent to
  • /rcpcollection/rcprecipe/rcpingredient/_at_amoun
    t
  • /descendant-or-selfnode()/ can be replaced with
    //, e.g., //rcprecipercptitleRicotta
    Pie//rcpingredientselects all ingredient
    nodes in any recipe whose title is Ricotta Pie
    no matter how deeply nested within the recipe
    node the ingredient node might be
  • selfnode() can be replaced with . and
    parentnode() can be replaced with ..
    e.g., /descendant-or-selfnode()/ childrcp.nu
    tritionattributecalories349/ parentnode()/
    childrcptitle/childtext()can be abbreviated
    to //rcpnutrition_at_calories349/../rcptitle/te
    xt()

29
Some subtleties with abbreviations
  • Any expression beginning with / is evaluated with
    the root node (not the root element) as the
    context, so //rcprecipe/rcpingredient//rcping
    redientreturns all ingredients that are not
    inside composite ingredients, whereas //rcprecip
    e/rcpingredient.//rcpingredientreturns all
    composite ingredients
  • Note also that //rcpingredient1selects all
    ingredient nodes that are first among their
    siblings whereas, /descendantrcpingredient1
    selects the first ingredient in the collection
    since predicate selects from single sequence of
    nodes that satisfies the axis and node test

30
General expressions
  • XPath has grown into a large language for
    expressing computations on sequences
  • XPath 2.0 has many features motivated by its use
    for selection within XQuery
  • Keywords and operators in XPath 2.0
  • , to union intersect except .
  • - div idiv mod and or
  • ! gt gt lt lt
  • eq ne lt le gt ge
  • is ltlt gtgt
  • for in if then else some every satisfies

31
Values and atomization
  • Every XPath expression evaluates to a sequence of
    items
  • Sequence may be empty
  • Each element in the sequence can be
  • A node
  • An atomic value which can be
  • A number
  • Integer, decimal, float or double
  • A boolean value
  • A string of Unicode characters
  • A datatype defined in XML Schema
  • Note that the result of an expression is always a
    sequence even if the sequence only contains one
    element
  • XPath interprets a single atomic value as a
    singleton sequence containing that value
  • Remember that a single node contains all its
    descendants and therefore denotes the subtree of
    which it is the root
  • Atomizing a sequence means converting it into a
    sequence of atomic values
  • This is done by converting every node into its
    string value thus
  • The string value of a text node is its contents
  • The string value of an element is the
    concatenation in document order of the string
    values of all descendant text nodes
  • The string value of an attribute node is the
    value of the attribute
  • The string value of a comment node is the comment
    text

32
Literal expressions
  • A literal expression is a singleton sequence
    containing a constant atomic value
  • Literal numbers written in expected way, thus
  • 42 is an integer
  • 3.1415 is a decimal
  • 6.022E23 is a float or double
  • Literal strings enclosed in single or double
    quotes, thus
  • 'XPath is a lot of fun'
  • "XPath is a lot of fun"
  • When string contains single or double quotes,
    there are two solutions
  • 'The cat said "Meow"'
  • "The cat said ""Meow""'
  • No literal boolean values
  • Use constant functions, true() and false()

33
Comments
  • Insert a comment into an XPath expression by
    using the following syntax
  • ( this is a comment )

34
Variable references
  • Variable references are written as follows
  • foo
  • refers to the variable foo
  • Variable may be
  • bound within context of expression
  • created through a binding in a for expression or
    quantified expression
  • Variable name may be any QName
  • Variable name may belong to a specific namespace
  • barfoo

35
Arithmetic expressions
  • For integers, decimals, floats and doubles
  • - div
  • -n (unary minus)
  • For integers
  • idiv (integer division)
  • mod (modulo)
  • Every argument is actually a sequence
  • if argument is the empty sequence, then result is
    an empty sequence
  • if all arguments are singleton sequences
    containing numbers of the expected type, then the
    operation is performed and the result is returned
    as a sequence
  • otherwise, a runtime error occurs
  • Variables are QNames and can therefore contain a
    minus sign (-)
  • Thus
  • foo-17 is a reference to a variable called
    foo-17
  • If we want to subtract 17 from foo, then we have
    to write
  • (foo)-17
  • foo -17
  • foo-17
  • or anything else that separates the foo from the
    17

36
Sequence expressions
  • If expi is an expression, then
  • exp1, exp2, exp3, ..., expn
  • constructs a new sequence which is the
    concatenation of all the expressions, expi
  • Atomization is always performed before
    concatenation, so it is impossible to produce
    nested sequences
  • Thus, (1, (2, 3, 4), ((5)), (), (((6, 7), 8),
    9))produces the same sequence as 1, 2, 3, 4, 5,
    6, 7, 8, 9
  • Expression exp1 to exp2 requires that exp1 and
    exp2 are both singleton sequences that evaluate
    to integers and whole expression evaluates to the
    sequence exp1, exp11, ..., exp2
  • e.g., sequence above could also be expressed
    as 1 to 9
  • Node sequences can be combined using set
    operators union (or ) intersect except
    (which means set difference)
  • each performs set operation and returns result
    sequence in document order containing no
    duplicate of identical nodes
  • For example, sequence exp can be sorted into
    document order with duplicates of identical nodes
    removed using the expression exp ()

37
Path expressions
  • Location paths are XPath expressions
  • A location path is evaluated in a sequence of
    location steps, starting with a context
  • Can also evaluate a location path relative to any
    arbitrary node sequence returned by some XPath
    expression
  • result is concatenation of results of evaluating
    location path with each node in the input
    sequence in turn
  • context position of each node in input sequence
    is position within input sequence
  • context size is length of input sequence
  • For example (fndoc("veggie.xml"),
    fndoc("bbq.xml"))//rcptitlereturns titles of
    all recipes in both files
  • fndoc function returns root node of a document

38
Filter expressions
  • Location path predicate is a special type of
    filter expression
  • Filter expression can be applied to any sequence
    containing nodes and/or atomic values
  • Syntax expfilterwhere
  • exp is an expression that evaluates to a sequence
  • filter is a filter expression that selects those
    elements in exp for which filter is true
  • inside filter, current item in exp is referred to
    by the symbol .
  • current context position is position within exp
  • current context size is size of exp
  • Example (30 to 60). mod 5 0 and
    position()gt20has the same result as the
    expression 50,55,60(remember that , is the
    symbol for concatenation in XPath)

39
Comparison expressions
  • There are three types of comparison expressions
    in XPath
  • Value comparisons
  • used to compare atomic values
  • General comparisons
  • can be used to compare all values
  • Node comparisons
  • used to compare nodes on identity and document
    order

40
Value comparisons
  • Value comparison operators are used to compare
    atomic values
  • Value comparison operators are eq ne lt le gt ge
  • When applied to two arbitrary values, following
    procedure carried out
  • the two values are atomized
  • if either resulting sequence is empty, the result
    is the empty sequence
  • if either sequence has more than one element, the
    result is a type error
  • if the two atomic values (represented by two
    singleton sequences) are not comparable (e.g., 7
    and "abc"), a runtime error occurs
  • otherwise the result is obtained by comparing the
    two atomic values
  • For example, the following expressions all
    evaluate to true8 eq 4 4//rcpdescription/tex
    t() eq "Some recipes used in the XML
    tutorial."(//rcpingredient)1/_at_name eq "beef
    cube steak"

41
General comparisons
  • General comparison operators used to compare all
    values
  • General comparison operators are
  • ! lt lt gt gt
  • When applied to two values, following steps
    performed
  • the two arguments are atomized
  • if there exists at least one pair of atomic
    values, one from each argument, for which the
    comparison holds, the result is true
  • otherwise the result is false
  • For example, the following all evaluate to true
  • 8 44
  • (1,2) (2,4)
  • (2,4) (3,4)
  • //rcpingredient/_at_name'salt'
  • This type of equality is not transitive (1,2)
    ! (3,4)even though (1,2) (2,4) and (2,4)
    (3,4)

42
Node comparisons
  • Node comparison operators used to compare nodes
    for identity and document order
  • Node comparison operators are
  • is arguments refer to the same node (identity)
  • ltlt first argument precedes second in document
    order
  • gtgt first argument follows second in document
    order
  • When applied to two arguments, following steps
    performed
  • if either sequence is empty, returns empty
    sequence
  • if both arguments are singleton sequences
    containing nodes, then the result is the boolean
    value of the comparison
  • otherwise a runtime error occurs
  • For example, following all evaluate to true
  • (//rcprecipe)2 is //rcprecipercptitle/text()
    eq "Ricotta Pie"
  • /rcpcollection ltlt (//rcprecipe)4
  • (//rcprecipe)4 gtgt (//rcprecipe3)
  • Note that, in an XSLT or XQuery file, the
    operators ltlt and gtgt must be rendered as ltlt
    and gtgt, respectively

43
Comparison confusions
  • Must always carefully consider whether a
    comparison should be a value, general or node
    comparison
  • making the wrong decision could lead to an
    unexpected result
  • For example, given that the 40th and 53rd
    ingredients in the recipes.xml file are different
    amounts of salt
  • (//rcpingredient)40/_at_name eq
    (//rcpingredient)53/_at_namereturns true but
  • (//rcpingredient)40/_at_name is
    (//rcpingredient)53/_at_namereturns false

44
Boolean expressions
  • Operators and and or accept arguments of any type
    which are then coerced to effective boolean
    values
  • Following are coerced to boolean value true
  • boolean value true
  • a non-empty string
  • a non-zero number
  • a sequence in which the first item is a node
  • Following coerced to boolean value false
  • boolean value false
  • empty string
  • 0
  • empty sequence
  • Otherwise, result is undefined or an error
  • Boolean values true and false can be constructed
    using the functions true() and false()
  • Boolean value can be negated using the function
    not(exp)

45
Functions
  • XPath 2.0 and XQuery 1.0 functions are defined in
    the proposed W3C recommendation which is
    available here
  • http//www.w3.org/TR/xpath-functions/
  • To use functions, context must contain
    declaration of the namespace
  • http//www.w3.org/2005/xpath-functions/
  • This URI is also the URL of a page that
    summarises all the available XPath 2.0 functions
  • This namespace is traditionally given the prefix
    fn in a namespace declaration
  • The XML Schema namespace, http//www.w3.org/2001/
    XMLSchemaalso defines some useful functions for
    coercion and constructing data values
  • XML Schema namespace traditionally given prefix
    xs

46
Arithmetic Functions
  • fnabs(-23.4) 23.4
  • fnceiling(-23.4) -23
  • fnfloor(23.4) 23
  • fnround(23.4) 23
  • fnround(-23.4) -23
  • fnround(23.5) 24

47
Boolean Functions
  • fnnot(0) fntrue()
  • fnnot(fntrue()) fnfalse()
  • fnnot("") fntrue()
  • fnnot((1)) fnfalse()
  • (1) evaluates to the number 1

48
String Functions
  • fnconcat("X","ML") "XML"
  • fnconcat("X","ML"," ","book") "XML book"
  • fnstring-join(("XML","book")," ") "XML book"
  • fnstring-join(("1","2","3"),"") "123"
  • fnsubstring("XML book",5) "book"
  • fnsubstring("XML book",2,4) "ML b"
  • fnstring-length("XML book") 8
  • fnupper-case("XML book") "XML BOOK"
  • fnlower-case("XML book") "xml book"

49
Regexp Functions
  • fncontains("XML book","XML") fntrue()
  • fnmatches("XML book","XM..a-z") fntrue()
  • fnmatches("XML book",".Z.") fnfalse()
  • fnreplace("XML book","XML","Web") "Web book"
  • fnreplace("XML book","a-z","8") "XML 8888"

50
Cardinality Functions
  • Following decide cardinality of general sequences
  • fnempty is the negation of fnexists
  • it returns false iff the argument is the empty
    sequence
  • fnexists(()) fnfalse()
  • fnexists((1,2,3,4)) fntrue()
  • fnempty(()) fntrue()
  • fnempty((1,2,3,4)) fnfalse()
  • fncount((1,2,3,4)) 4
  • fncount(//rcprecipe) 5

51
Sequence Functions
  • Argument sequence must be enclosed in
    parentheses, otherwise function treats each
    element as a separate argument
  • makes sense, since comma used to separate
    arguments!
  • fndistinct-values((1, 2, 3, 4, 3, 2)) (1, 2,
    3, 4)
  • removes duplicates using eq for atoms and is for
    nodes
  • order of result is implementation-dependent
  • fninsert-before((2, 4, 6, 8), 2, (3, 5)) (2,
    3, 5, 4, 6, 8)
  • fnremove((2, 4, 6, 8, 10), 3) (2, 4, 8, 10)
  • fnreverse((2, 4, 6, 8)) (8, 6, 4, 2)
  • fnsubsequence((2, 4, 6, 8, 10), 2) (4, 6, 8,
    10)
  • fnsubsequence((2, 4, 6, 8, 10), 2, 3) (4, 6,
    8)

52
Aggregate Functions
  • fnavg((2, 3, 4, 5, 6, 7)) 4.5
  • fnmax((2, 3, 4, 5, 6, 7)) 7
  • fnmin((2, 3, 4, 5, 6, 7)) 2
  • fnsum((2, 3, 4.5, 5, 6, 7)) 27.5

53
Node Functions
  • fndoc("http//www.brics.dk/ixwt/recipes/recipes.x
    ml")
  • reads in document and returns root node
  • fnposition()
  • returns current context position
  • fnlast()
  • returns current context size
  • Examples
  • fndoc("recipes.xml")//rcprecipefnposition()2
    /rcptitle/text()
  • returns
  • Ricotta Pie
  • fndoc("recipes.xml")//rcprecipefnlast()/rcpt
    itle/text()
  • returns
  • Cailles en Sarcophages
  • fndoc("http//www.brics.dk/ixwt/recipes/recipes.x
    ml") is
  • fndoc("http//www.brics.dk/ixwt/recipes/recipes.x
    ml")

54
Coercion Functions
  • xsinteger("5") 5
  • xsinteger(7.0) 7
  • xsdecimal(5) 5.0
  • xsdecimal("4.3") 4.3
  • xsdecimal("4") 4.0
  • xsdouble(2) 2.0E0
  • xsdouble(14.3) 1.43E1
  • xsboolean(0) fnfalse()
  • xsboolean("0") fntrue()
  • xsboolean("false") fntrue()
  • xsstring(17) "17"
  • xsstring(1.43E1) "14.3"
  • xsstring(fntrue()) "true"

55
For Expressions
  • The expression
  • for r in //rcprecipe
  • return fncount(r//rcpingredientfnnot(rcpi
    ngredient))
  • returns the number of simple ingredients in each
    recipe
  • 11, 12, 15, 8, 30
  • The expression
  • for i in (1 to 5)
  • for j in (1 to i)
  • return j
  • returns the value
  • 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5

56
Conditional Expressions
  • if (exp1) then exp2 else exp3
  • Following returns average amount in millilitres
    of all ingredients measured in cups, teaspoons or
    tablespoons
  • fnavg(
  • for i in //rcpingredient return
  • if ( i/_at_unit "cup" )
  • then xsdouble(i/_at_amount) 237
  • else if ( i/_at_unit "teaspoon" )
  • then xsdouble(i/_at_amount) 5
  • else if ( i/_at_unit "tablespoon" )
  • then xsdouble(i/_at_amount) 15
  • else ()
  • )
  • If ingredient does not contain a unit attribute,
    then _at_unit () and ()"cup" is equal to false

57
Quantified Expressions
  • some r in //rcpingredient
  • satisfies r/_at_name eq "sugar"
  • returns true if sugar is an ingredient in any of
    the recipes
  • Above expression is equivalent to
  • fnexists(
  • for r in //rcpingredient return
  • if (r/_at_name eq "sugar") then fntrue() else
    ()
  • )

58
XPath 1.0 Restrictions
  • Many implementations only support XPath 1.0
  • Incorrect handling of default namespaces
  • Smaller function library
  • Implicit casts of values
  • Some expressions change semantics "4" lt
    "4.0"is false in XPath 1.0 (because it
    implicitly casts the values to numbers) but true
    in XPath 2.0 (which treats the arguments as
    strings and compares them in terms of
    lexicographic order)

59
Summary
  • Being able select and navigate to nodes in an XML
    tree is essential if we are to perform
    computations on the data
  • This functionality has been factored out into the
    XPath language
  • XPath location path consists of a sequence of
    location steps, each of which consists of an
    axis, node step and zero or more predicates
  • Location paths can be abbreviated using various
    conventions and special symbols
  • A location step maps a sequence of nodes onto
    another sequence of nodes
  • XPath expressions are used in XSLT and XQuery
  • XPath expressions evaluate to sequences
    containing nodes and atomic values
  • A large collection of functions have been defined
    for use in XPath expressions which allow for it
    to be used for carrying out complex computations
    on sequences
Write a Comment
User Comments (0)
About PowerShow.com