SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances (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
 

Similaire à 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
 

Plus 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
 

Plus 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
 

Dernier

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
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
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
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
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
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
 

Dernier (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
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
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
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
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
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
 

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