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

Más contenido relacionado

La actualidad más candente (20)

Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
 
X FILES
X FILESX FILES
X FILES
 
Querring xml with xpath
Querring xml with xpath Querring xml with xpath
Querring xml with xpath
 
Rendering XML Document
Rendering XML DocumentRendering XML Document
Rendering XML Document
 
Xpath
XpathXpath
Xpath
 
Day2 xslt x_path_xquery
Day2 xslt x_path_xqueryDay2 xslt x_path_xquery
Day2 xslt x_path_xquery
 
Xpath1
Xpath1Xpath1
Xpath1
 
XQuery - a technical overview
XQuery -  a technical overviewXQuery -  a technical overview
XQuery - a technical overview
 
XSLT presentation
XSLT presentationXSLT presentation
XSLT presentation
 
XML/XSLT
XML/XSLTXML/XSLT
XML/XSLT
 
Xslt tutorial
Xslt tutorialXslt tutorial
Xslt tutorial
 
XSLT
XSLTXSLT
XSLT
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
transforming xml using xsl and xslt
transforming xml using xsl and xslttransforming xml using xsl and xslt
transforming xml using xsl and xslt
 
Xml and Co.
Xml and Co.Xml and Co.
Xml and Co.
 
linked_lists4
linked_lists4linked_lists4
linked_lists4
 
linked_lists
linked_listslinked_lists
linked_lists
 
Xml transformation language
Xml transformation languageXml transformation language
Xml transformation language
 
Xslt elements
Xslt elementsXslt elements
Xslt elements
 

Similar a XPath Introduction (20)

Xml 1
Xml 1Xml 1
Xml 1
 
Learning XSLT
Learning XSLTLearning XSLT
Learning XSLT
 
Xml
XmlXml
Xml
 
Well Formed XML
Well Formed XMLWell Formed XML
Well Formed XML
 
CIS-189 Final Review
CIS-189 Final ReviewCIS-189 Final Review
CIS-189 Final Review
 
XML Transformations With PHP
XML Transformations With PHPXML Transformations With PHP
XML Transformations With PHP
 
Overview of XSL, XPath and XSL-FO
Overview of XSL, XPath and XSL-FOOverview of XSL, XPath and XSL-FO
Overview of XSL, XPath and XSL-FO
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Xml
XmlXml
Xml
 
Basic xml syntax
Basic xml syntaxBasic xml syntax
Basic xml syntax
 
chapter 4 web authoring unit 4 xml.pptx
chapter 4 web authoring  unit 4 xml.pptxchapter 4 web authoring  unit 4 xml.pptx
chapter 4 web authoring unit 4 xml.pptx
 
EXtensible Markup Language
EXtensible Markup LanguageEXtensible Markup Language
EXtensible Markup Language
 
Xml
XmlXml
Xml
 
Xml1111
Xml1111Xml1111
Xml1111
 
Xml Presentation-3
Xml Presentation-3Xml Presentation-3
Xml Presentation-3
 
XML
XMLXML
XML
 
Ch2 neworder
Ch2 neworderCh2 neworder
Ch2 neworder
 
Xml intro1
Xml intro1Xml intro1
Xml intro1
 
1 xml fundamentals
1 xml fundamentals1 xml fundamentals
1 xml fundamentals
 
Xmlphp
XmlphpXmlphp
Xmlphp
 

Más de Stuart Myles

IPTC Rights Statements For News
IPTC Rights Statements For NewsIPTC Rights Statements For News
IPTC Rights Statements For NewsStuart Myles
 
IPTC New Taxonomies Ideas
IPTC New Taxonomies IdeasIPTC New Taxonomies Ideas
IPTC New Taxonomies IdeasStuart Myles
 
IPTC Board Spring 2019
IPTC Board Spring 2019IPTC Board Spring 2019
IPTC Board Spring 2019Stuart Myles
 
IPTC Spring 2019 Conference
IPTC Spring 2019 ConferenceIPTC Spring 2019 Conference
IPTC Spring 2019 ConferenceStuart Myles
 
Photomation or Fauxtomation?
Photomation or Fauxtomation?Photomation or Fauxtomation?
Photomation or Fauxtomation?Stuart Myles
 
Image Tagging at the Associated Press
Image Tagging at the Associated PressImage Tagging at the Associated Press
Image Tagging at the Associated PressStuart Myles
 
IPTC Rights Working Group Toronto October 2018
IPTC Rights Working Group Toronto October 2018IPTC Rights Working Group Toronto October 2018
IPTC Rights Working Group Toronto October 2018Stuart Myles
 
IPTC AGM 2018 Welcome
IPTC AGM 2018 WelcomeIPTC AGM 2018 Welcome
IPTC AGM 2018 WelcomeStuart Myles
 
How Can We Make Algorithmic News More Transparent?
How Can We Make Algorithmic News More Transparent?How Can We Make Algorithmic News More Transparent?
How Can We Make Algorithmic News More Transparent?Stuart Myles
 
IPTC EXTRA Spring 2018
IPTC EXTRA Spring 2018IPTC EXTRA Spring 2018
IPTC EXTRA Spring 2018Stuart Myles
 
IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...
IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...
IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...Stuart Myles
 
Ap Taxonomy Localization Requirements and Challenges
Ap Taxonomy Localization Requirements and ChallengesAp Taxonomy Localization Requirements and Challenges
Ap Taxonomy Localization Requirements and ChallengesStuart Myles
 
IPTC Spring Meeting Welcome To Athens April 2018
IPTC Spring Meeting Welcome To Athens April 2018IPTC Spring Meeting Welcome To Athens April 2018
IPTC Spring Meeting Welcome To Athens April 2018Stuart Myles
 
Sustaining Television News Technical Challenges
Sustaining Television News Technical ChallengesSustaining Television News Technical Challenges
Sustaining Television News Technical ChallengesStuart Myles
 
How to Train Your Classifier: Create a Serverless Machine Learning System wit...
How to Train Your Classifier: Create a Serverless Machine Learning System wit...How to Train Your Classifier: Create a Serverless Machine Learning System wit...
How to Train Your Classifier: Create a Serverless Machine Learning System wit...Stuart Myles
 
The Search for IPTC's Next Managing Director
The Search for IPTC's Next Managing DirectorThe Search for IPTC's Next Managing Director
The Search for IPTC's Next Managing DirectorStuart Myles
 
IPTC Approach to News in JSON
IPTC Approach to News in JSONIPTC Approach to News in JSON
IPTC Approach to News in JSONStuart Myles
 
IPTC News in JSON November 2017
IPTC News in JSON November 2017IPTC News in JSON November 2017
IPTC News in JSON November 2017Stuart Myles
 
IPTC EXTRA and EXTRA+ November 2017
IPTC EXTRA and EXTRA+ November 2017IPTC EXTRA and EXTRA+ November 2017
IPTC EXTRA and EXTRA+ November 2017Stuart Myles
 
Welcome to Barcelona - IPTC November 2017
Welcome to Barcelona - IPTC November 2017Welcome to Barcelona - IPTC November 2017
Welcome to Barcelona - IPTC November 2017Stuart Myles
 

Más de Stuart Myles (20)

IPTC Rights Statements For News
IPTC Rights Statements For NewsIPTC Rights Statements For News
IPTC Rights Statements For News
 
IPTC New Taxonomies Ideas
IPTC New Taxonomies IdeasIPTC New Taxonomies Ideas
IPTC New Taxonomies Ideas
 
IPTC Board Spring 2019
IPTC Board Spring 2019IPTC Board Spring 2019
IPTC Board Spring 2019
 
IPTC Spring 2019 Conference
IPTC Spring 2019 ConferenceIPTC Spring 2019 Conference
IPTC Spring 2019 Conference
 
Photomation or Fauxtomation?
Photomation or Fauxtomation?Photomation or Fauxtomation?
Photomation or Fauxtomation?
 
Image Tagging at the Associated Press
Image Tagging at the Associated PressImage Tagging at the Associated Press
Image Tagging at the Associated Press
 
IPTC Rights Working Group Toronto October 2018
IPTC Rights Working Group Toronto October 2018IPTC Rights Working Group Toronto October 2018
IPTC Rights Working Group Toronto October 2018
 
IPTC AGM 2018 Welcome
IPTC AGM 2018 WelcomeIPTC AGM 2018 Welcome
IPTC AGM 2018 Welcome
 
How Can We Make Algorithmic News More Transparent?
How Can We Make Algorithmic News More Transparent?How Can We Make Algorithmic News More Transparent?
How Can We Make Algorithmic News More Transparent?
 
IPTC EXTRA Spring 2018
IPTC EXTRA Spring 2018IPTC EXTRA Spring 2018
IPTC EXTRA Spring 2018
 
IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...
IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...
IPTC Machine Readable Rights for News and Media: Solving Three Challenges wit...
 
Ap Taxonomy Localization Requirements and Challenges
Ap Taxonomy Localization Requirements and ChallengesAp Taxonomy Localization Requirements and Challenges
Ap Taxonomy Localization Requirements and Challenges
 
IPTC Spring Meeting Welcome To Athens April 2018
IPTC Spring Meeting Welcome To Athens April 2018IPTC Spring Meeting Welcome To Athens April 2018
IPTC Spring Meeting Welcome To Athens April 2018
 
Sustaining Television News Technical Challenges
Sustaining Television News Technical ChallengesSustaining Television News Technical Challenges
Sustaining Television News Technical Challenges
 
How to Train Your Classifier: Create a Serverless Machine Learning System wit...
How to Train Your Classifier: Create a Serverless Machine Learning System wit...How to Train Your Classifier: Create a Serverless Machine Learning System wit...
How to Train Your Classifier: Create a Serverless Machine Learning System wit...
 
The Search for IPTC's Next Managing Director
The Search for IPTC's Next Managing DirectorThe Search for IPTC's Next Managing Director
The Search for IPTC's Next Managing Director
 
IPTC Approach to News in JSON
IPTC Approach to News in JSONIPTC Approach to News in JSON
IPTC Approach to News in JSON
 
IPTC News in JSON November 2017
IPTC News in JSON November 2017IPTC News in JSON November 2017
IPTC News in JSON November 2017
 
IPTC EXTRA and EXTRA+ November 2017
IPTC EXTRA and EXTRA+ November 2017IPTC EXTRA and EXTRA+ November 2017
IPTC EXTRA and EXTRA+ November 2017
 
Welcome to Barcelona - IPTC November 2017
Welcome to Barcelona - IPTC November 2017Welcome to Barcelona - IPTC November 2017
Welcome to Barcelona - IPTC November 2017
 

Último

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Último (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

XPath Introduction

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