XPath - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

XPath

Description:

to address parts of an XML document. ... be returned from a remote document and embedded in a new or current document) ... you want to move in the document ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 20
Provided by: stevec2
Category:
Tags: xpath

less

Transcript and Presenter's Notes

Title: XPath


1
XPath
  • Navigating XML

2
Overview
  • Xpath is a non-xml syntax to be used with XSLT
    and Xpointer. Its purpose according to the W3.org
    is
  • to address parts of an XML document. In support
    of this primary purpose, it also provides basic
    facilities for manipulation of strings, numbers
    and booleans. XPath uses a compact, non-XML
    syntax to facilitate use of XPath within URIs and
    XML attribute values. XPath operates on the
    abstract, logical structure of an XML document,
    rather than its surface syntax. XPath gets its
    name from its use of a path notation as in URLs
    for navigating through the hierarchical structure
    of an XML document.

3
The Xpath Expression
  • An Xpath expression consists of a location path
    and one or more location steps.
  • Each location step consists of an axis, a node
    test and a predicate
  • axisnodetestpredicate
  • One or more of these elements may be absent but
    if both axis and nodetest are present they must
    be separated by and if the predicate is
    present it must be in

4
First a Few Terms
  • Node
  • Node-set
  • Context Node
  • Location Path

5
Node
  • There are seven types of nodes
  • The document as a whole
  • Element nodes (one for each element in a
    document)
  • Attribute nodes
  • Comment nodes
  • Processing instructions
  • Namespace nodes
  • Text nodes

6
Node-set
  • A node-set is a group of nodes returned by a
    single xpath expression
  • (This has important implications for Xpointers
    where sets of information could be returned from
    a remote document and embedded in a new or
    current document)

7
Context-node
  • The context node identifies where in the document
    to start applying a given set of instructions

8
Location Path
  • The location path is simply the path or
    instructions for getting to a particular point in
    a document

9
Axis
  • The axis determines which direction you want to
    move in the document
  • Directions are like next node, preceding node,
    Parent node, Child node

10
Some Axis Directions
11
Axis Short Cut Syntax
12
Nodetest
  • There are three kinds of node test
  • Name testtests the names along the specified
    path
  • Node typestests for the type of node along the
    specified path (comment, node, processing-instruct
    ion or text)
  • Literal textlocates the literal text
  • precedingprocessing-instruction(xmlstylesheet
    )

13
Predicate
  • The predicate part is optional, but can give you
    more precise control
  • //customer_at_name'John'/_at_name
  • This has a predicate of _at_nameJohn which
    selects the order that has the name attribute
    value John

14
Xpath Function
  • Xpath also contains many built in functions. Some
    are

15
An Example The XML File
  • lt?xml version1.0?gtltordersgt ltcustomer
    nameJohn gt ltorder quantity5gt dinner
    specials lt/ordergt ltorder quantity2gt
    red wine lt/ordergt lt/customergtlt/ordersgt
  • click here to see the whole file

16
Selecting one Customer
  • lth3gtltxslvalue-of select"//customer_at_name'John'
    /_at_name"/gtlt/h3gt
  • ltpgt
  • ltxslfor-each select"//customer_at_name'John'/ord
    er"gt
  • ltxslvalue-of select"_at_quantity" /gt
  • ltxslvalue-of select"." /gt
  • lt/xslfor-eachgt
  • lt/pgt

17
Totaling the orders
  • ltxslvalue-of select"sum(//order/_at_quantity)" /gt

18
Returning the Node Names
  • ltxslfor-each select"//customer"gt
  • ltxslvalue-of select"name()"/gt
  • lt/xslfor-eachgt

19
Count of Nodes
  • ltxslvalue-of select"count(customer)" /gt
Write a Comment
User Comments (0)
About PowerShow.com