SGMLXML.net A place for SGML and XML application developers.

April 6, 2006

Quick tour of XPath

Filed under: XSLT — cangione @ 3:43 pm







Getting lots of questions about XPath on the #XML IRC channel so figured I’d write a quick cheat sheet. Sometimes the hardest part of learning something is knowing what it’s called.

There are 7 Types of Nodes:

  1. Root Node
    /
  2. Element Node
  3. Text Node text
    ()
  4. Attribute Node
    @
  5. Comment Node
    comment()
  6. Processing Instruction
    processing-instruction()
  7. Namespace Node

There are 13 AXIS Specifiers from your current node:

  1. ancestor::
  2. ancestor-or-self::
  3. attribute::(or @)
  4. child::(or nothing)
  5. descendant::
  6. descendant-or-self::
  7. following::
  8. following-sibling::
  9. namespace::
  10. parent::or (or ..)
  11. preceding::
  12. preceding-sibling::
  13. self:: or (.)

Note that the double colon "::" immediately follows the use of an axis name.

Predicates:

Predicates are used to qualify expressions (i.e filter nodes). Predicates are specifiable on any node set and appear in sqare brackets after the node test.

Example:

version[3]

Functions:

  • last()

    – When processing a list of nodes, if the nodes are numbered from one, last() returns the last number.

  • position()

    – returns the value of the context position

  • name()

    – returns the name of a node. Good for getting things like the element name for tests.

  • concat() 

    – take two or more arguments and joins the results together end-to-end.

  • count()

    – returns the number of nodes present in the node-set

  • not()

    – returns the negation of its argument.

Examples:

Actually reading the XPath Expressions is probably the easiest way to learn them. Keep in mind that from any position in the document you can write an XPath expression to get to any other node in that document.

para
matches all para children in the current context
para/emphasis
matches all emphasis elements that have a parent of para
//title
matches all title elements anywhere in the document
.//title 
matches all title elements that are descendants of the current context
item[1]
matches the "first" item
item[position()=last()]
matches the last item (in a given list)
topic[@level='basic']
matches topic elements with level attributes whose value is basic
//chapter[2]/title
The title of the second chapter
preceding::partNumber
all preceding partNumber elements


No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress