XPath: An Introduction<br />Stuart Myles<br />
Objectives<br />What is XPath?<br />An introduction to the XPath 1.0 language<br />XML refresher<br />XPath basics<br />Wh...
XPathXML Path Language<br />Path notation with slashes<br />newsItem/rightsInfo/copyrightHolder<br />recipe/ingredientList...
What is XPath?<br />Syntax for defining parts of an XML document<br />Locate elements or attributes<br />Performing operat...
XPath Introduction:XML Refresher<br />XML documents contain one or more elements, delimited by start and end tags<br /><fo...
XML Attributes and Text Content<br />Elements can have attributes<br /><foo lang=“fr”><br />	  <bar id=“theOne” lang=“en”>...
XML Namespaces<br />Elements can be defined in different namespaces<br />Namespaces look like URLs<br />You can use xmlns ...
XML Namespace Prefixes<br />You can use xmlns:prefix to declare a namespace and bind it to a prefix<br /><nar:newsItemxmln...
XPath Crash CourseThe Basics: Selecting Elements<br />The simplest XPath form:<br />one or more tag names, separated by sl...
XPath: Using Attributes<br />Attribute values are indicated by @<br />@rel<- The rel attribute of the current element<br /...
XPath and Namespaces<br />XPath supports namespaces<br />nitf:p <- The p element from the nitf namespace<br />xhtml:p <- T...
What Else Can XPath Do?Numeric, String, Boolean Functions<br />Publication/FilingMetadata[1]<br />Publication/FilingMetada...
More XPath Information<br />List of examples:<br />http://msdn.microsoft.com/en-us/library/ms256086.aspx<br />Introductory...
Upcoming SlideShare
Loading in …5
×

XPath Introduction

2,317 views

Published on

An introduction to XPath, including a refresher on XML and the key parts of the XPath standard.

Published in: Technology
  • Be the first to comment

XPath Introduction

  1. 1. XPath: An Introduction<br />Stuart Myles<br />
  2. 2. Objectives<br />What is XPath?<br />An introduction to the XPath 1.0 language<br />XML refresher<br />XPath basics<br />What else can you do with XPath 1.0?<br />Where to go for more information<br />
  3. 3. XPathXML Path Language<br />Path notation with slashes<br />newsItem/rightsInfo/copyrightHolder<br />recipe/ingredientList/ingredient<br />Like UNIX directory paths or URLS<br />
  4. 4. What is XPath?<br />Syntax for defining parts of an XML document<br />Locate elements or attributes<br />Performing operations over data<br />XPath contains a library of standard functions<br />Numeric, string, boolean<br />A major part of several XML standards<br />XSLT, XQuery, XML Schema, Schematron<br />
  5. 5. XPath Introduction:XML Refresher<br />XML documents contain one or more elements, delimited by start and end tags<br /><foo><br /></foo><br />Elements can be nested to any depth<br /><foo><br /> <bar></bar><br /></foo><br />
  6. 6. XML Attributes and Text Content<br />Elements can have attributes<br /><foo lang=“fr”><br /> <bar id=“theOne” lang=“en”></bar><br /></foo><br />Elements can have text content<br /><foo lang=“fr”><br /> <bar lang=“en”>theOne</bar><br /></foo><br />Empty elements have no children or text<br /><foo></foo><br />A shorthand for writing empty elements<br /><foo /><br />
  7. 7. XML Namespaces<br />Elements can be defined in different namespaces<br />Namespaces look like URLs<br />You can use xmlns to declare a default namespace<br /><newsItemxmlns='http://iptc.org/std/nar/2006-10-01/'><br />  <itemMeta>   <title>Pope Blesses Astronauts</title><br /> </itemMeta></newsItem><br />newsItemis in the http://iptc.org/std/nar/2006-10-01/namespace<br />itemMeta and title are also in the http://iptc.org/std/nar/2006-10-01/ ns<br />Child elements inherit from their parents<br />
  8. 8. XML Namespace Prefixes<br />You can use xmlns:prefix to declare a namespace and bind it to a prefix<br /><nar:newsItemxmlns:nar='http://iptc.org/std/nar/2006-10-01/'>  <nar:itemMeta><br />   <nar:title>Pope Blesses Astronauts</nar:title><br /> </nar:itemMeta></nar:newsItem><br />newsItem is in the http://iptc.org/std/nar/2006-10-01/ namespace<br />itemMeta and title are also in the http://iptc.org/std/nar/2006-10-01/ namespace<br />To an XML parser, this document and the previous one are identical<br />
  9. 9. XPath Crash CourseThe Basics: Selecting Elements<br />The simplest XPath form:<br />one or more tag names, separated by slashes (/)<br />newsItem/itemMeta/title<- title under itemMeta<br />Use * instead of a tag name to match anything<br />newsItem/*/title <- title grandchildren of newsItem<br />An empty tag searches all levels of the tree<br />//title Every title element in the doc<br />newsItem//title Every title under newsItem<br />
  10. 10. XPath: Using Attributes<br />Attribute values are indicated by @<br />@rel<- The rel attribute of the current element<br />Element and Attribute values are tied by /@<br />link/@rel<- The rel attribute of the link element<br />Use [] for conditional selections<br />link[@rel] <- link element with a rel attribute<br />link[@rel = “parent”]<br />link[@size &lt; “1000”]<br />link[not(@href)]<br />
  11. 11. XPath and Namespaces<br />XPath supports namespaces<br />nitf:p <- The p element from the nitf namespace<br />xhtml:p <- The p element from the xhtml ns<br />nar:* <- Any element from the nar namespace<br />@atom:* <- Any attribute from the atom ns<br />Protip: if you can’t figure out why your XPath<br />expression isn’t matching, check the namespace<br />
  12. 12. What Else Can XPath Do?Numeric, String, Boolean Functions<br />Publication/FilingMetadata[1]<br />Publication/FilingMetadata[last()]<br />Publication/FilingMetadata[last() - 1]<br />FilingMetadata[position() mod 2 = 0]<br />FilingMetadata[Category = “q” or Category = “j”]<br />not(contains(SlugLine, “advisory”))<br />starts-with(FilingOnlineCode, “1”)<br />And XPath 2.0 adds even more functions, including regular expressions<br />
  13. 13. More XPath Information<br />List of examples:<br />http://msdn.microsoft.com/en-us/library/ms256086.aspx<br />Introductory, interactive tutorial:<br />http://www.zvon.org/comp/r/tut-XPath_1.html<br />More advanced tutorial:<br />http://www.ibm.com/developerworks/xml/tutorials/x-xpath/section2.html<br />XPath chapter from XML in a Nutshell:<br />http://oreilly.com/catalog/xmlnut/chapter/ch09.html<br />

×