SlideShare a Scribd company logo
Introduction to Perl & XPATH
What is XPATH
XPATH Terminology Items Nodes Atomic values Relationship of Nodes Parent Children Siblings Ancestors Descendants
XPATH Syntax != div - + * >= > = <= < or and | [] @ .. . // / Nodename
XML Sample <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Mastering Perl</title> <author>brian d foy</author> <year>2007</year> <price>39.99</price> </node> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Perl Best Practices</title> <author>Damian Conway</author> <year>2005</year> <price>39.95</price> </node> <node subject=&quot;OO&quot;> <title lang=&quot;en&quot;>Design Patterns: Elements of Reusable Object-Oriented Software</title> <author>Erich Gamma</author> <author>Richard Helm</author> <author>Ralph Johnson</author> <author>John Vlissides</author> <year>1994</year> <price>59.99</price> </node> <node subject=&quot;RegEx&quot;> <title lang=&quot;en&quot;>Mastering Regular Expressions , Third Edition</title> <author>Jeffrey E. F. Friedl</author> <year>2006</year> <price>44.99</price> </node> </root>
Sample Script use strict; use warnings; use XML::LibXML;  # To the daring win32 perl people: use the ppm command line # to install this package, the gui still doesn’t give the right answers # to the setup procedure. #### Fetch libxml2.dll? [yes] yes #### Where should libxml2.dll be placed? [C:\Perl\bin] <ENTER> my $RawXml  = '<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> .. </root>'; my $parser = XML::LibXML->new(); my $tree  = $parser->parse_string($RawXml); my $root  = $tree->getDocumentElement(); .. foreach my $N ( $bookshelf->findnodes(' XPATH_EXPRESSION ') ) { #  print $N. &quot;\n&quot;;  # XML::LibXML::Element=SCALAR(0x1a7a7d4) print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; }
Searching throughout on from the current  (xml libxml object)  node use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('//title') ) {
Searching throughout the specified xml tree path use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('book') ) { foreach my $N ( $bookshelf->findnodes('title') ) {  # won't work since no 'title' child objects at this xml tree level foreach my $N ( $bookshelf->findnodes('book/title') ) { foreach my $N ( $bookshelf->findnodes('/bookshelf/book/price') ) {
Playing with XPath positions use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('book[1]') ) { foreach my $N ( $bookshelf->findnodes('book[1]/title') ) { foreach my $N ( $bookshelf->findnodes('book[position()<3]') ) { foreach my $N ( $bookshelf->findnodes('book[last()-1]') ) { foreach my $N ( $bookshelf->findnodes('book[last()]') ) {
Querying element values use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('/bookshelf/book[price>40 and price<50]/title') ) { foreach my $N ( $bookshelf->findnodes('/bookshelf/book[price>50]/title') ) {
Querying attributes use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('//@lang') ) {  foreach my $N ( $bookshelf->findnodes('//title[@id]') ) { foreach my $N ( $bookshelf->findnodes('//title[@lang]') ) { foreach my $N ( $bookshelf->findnodes('//title[@id | @ooo]') ) { foreach my $N ( $bookshelf->findnodes( ' //title[@id= &quot; en &quot; ] ' ) ) {
Examples
XML Sample <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Mastering Perl</title> <author>brian d foy</author> <year>2007</year> <price>39.99</price> </node> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Perl Best Practices</title> <author>Damian Conway</author> <year>2005</year> <price>39.95</price> </node> <node subject=&quot;OO&quot;> <title lang=&quot;en&quot;>Design Patterns: Elements of Reusable Object-Oriented Software</title> <author>Erich Gamma</author> <author>Richard Helm</author> <author>Ralph Johnson</author> <author>John Vlissides</author> <year>1994</year> <price>59.99</price> </node> <node subject=&quot;RegEx&quot;> <title lang=&quot;en&quot;>Mastering Regular Expressions , Third Edition</title> <author>Jeffrey E. F. Friedl</author> <year>2006</year> <price>44.99</price> </node> </root>
Sample Script header use strict; use warnings; use XML::LibXML; my $RawXml  = '<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> .. </root>'; my $parser = XML::LibXML->new(); my $tree  = $parser->parse_string($RawXml); my $root  = $tree->getDocumentElement();
Find all nodes ## Find all nodes foreach my $N ( $root->findnodes('node') ) { print $N. &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  Output Sample: ##  XML::LibXML::Element=SCALAR(0x1a7a7d4) ##  node=&quot; ##  Mastering Perl ##  brian d foy ##  2007 ##  39.99 ##  &quot; ##  XML::LibXML::Element=SCALAR(0x1a7a7f4) ##  node=&quot; ##  Perl Best Practices ##  Damian Conway ##  2005 ##  39.95 ##  &quot; ##  XML::LibXML::Element=SCALAR(0x1a7a814) ##  node=&quot; ##  Design Patterns: Elements of Reusable Object-Oriented Software ##  Erich Gamma ##  Richard Helm ##  Ralph Johnson ##  John Vlissides ##  1994 ##  59.99 ##  &quot; ##  XML::LibXML::Element=SCALAR(0x1a7a834) ##  node=&quot; ##  Mastering Regular Expressions , Third Edition ##  Jeffrey E. F. Friedl ##  2006 ##  44.99 ##  &quot;
Find first node ## Find first node foreach my $N ( $root->findnodes('node[1]') ) { print $N->getAttribute(&quot;subject&quot;). &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  Perl ##  node=&quot; ##  Mastering Perl ##  brian d foy ##  2007 ##  39.99 ##  &quot;
Find last node ## Find last node foreach my $N ( $root->findnodes('node[last()]') ) { print $N->getAttribute(&quot;subject&quot;). &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  RegEx ##  node=&quot; ##  Mastering Regular Expressions , Third Edition ##  Jeffrey E. F. Friedl ##  2006 ##  44.99 ##  &quot;
Find last-1 node ## Find last-1 node foreach my $N ( $root->findnodes('node[last()-1]') ) { print $N->getAttribute(&quot;subject&quot;). &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  OO ##  node=&quot; ##  Design Patterns: Elements of Reusable Object-Oriented Software ##  Erich Gamma ##  Richard Helm ##  Ralph Johnson ##  John Vlissides ##  1994 ##  59.99 ##  &quot;
Find all titles ## Find all titles foreach my $N ( $root->findnodes('//title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Mastering Perl&quot; ##  title=&quot;Perl Best Practices&quot; ##  title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot; ##  title=&quot;Mastering Regular Expressions , Third Edition&quot; ##  title=&quot;Perl Best Practices duplicate&quot;
Find all node/titles ## Find all node/titles foreach my $N ( $root->findnodes('node/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Mastering Perl&quot; ##  title=&quot;Perl Best Practices&quot; ##  title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot; ##  title=&quot;Mastering Regular Expressions , Third Edition&quot;
Find all nodes/prices/text ## Find all nodes/prices foreach my $N ( $root->findnodes('/root/node/price/text()') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  #text=&quot;39.99&quot; ##  #text=&quot;39.95&quot; ##  #text=&quot;59.99&quot; ##  #text=&quot;44.99&quot;
Find all nodes/prices ## Find all nodes/prices foreach my $N ( $root->findnodes('/root/node/price') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  price=&quot;39.99&quot; ##  price=&quot;39.95&quot; ##  price=&quot;59.99&quot; ##  price=&quot;44.99&quot;
Find first node/title ## Find first node/title foreach my $N ( $root->findnodes('node[1]/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Mastering Perl&quot;
Find all nodes/titles where price>50 ## Find all nodes/titles where price>50 foreach my $N ( $root->findnodes('/root/node[price>50]/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot;
Find all nodes/titles where price<50 and price>40 ## Find all nodes/titles where price<50 and price>40 foreach my $N ( $root->findnodes('/root/node[price>40 and price<50]/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Mastering Regular Expressions , Third Edition“ ##  title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot;
Find all values for the attribute 'lang' ## Find all values for the attribute 'lang' foreach my $N ( $root->findnodes('//@lang') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  lang=&quot;en&quot; ##  lang=&quot;en&quot; ##  lang=&quot;en&quot; ##  lang=&quot;en&quot;
Find all titles with the attribute 'lang' ## Find all titles with the attribute 'lang' foreach my $N ( $root->findnodes('//title[@lang]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Mastering Perl&quot; ##  title=&quot;Perl Best Practices&quot; ##  title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot; ##  title=&quot;Mastering Regular Expressions , Third Edition&quot;
Find all titles with the attribute 'id' ## Find all titles with the attribute 'id' foreach my $N ( $root->findnodes('//title[@id]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Perl Best Practices duplicate&quot;
Find all titles with the attribute 'id=&quot;en&quot;' foreach my $N ( $root->findnodes('//title[@id=&quot;en&quot;]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Perl Best Practices duplicate&quot;
Find all titles with the attribute 'id' or with the attribute 'ooo' ## Find all titles with the attribute 'id' or with the attribute 'ooo' foreach my $N ( $root->findnodes('//title[@id | @ooo]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ##  title=&quot;Perl Best Practices duplicate&quot;
[email_address]

More Related Content

PDF
Working with text, Regular expressions
PPT
Perl Presentation
PPT
Class 5 - PHP Strings
PPTX
php string-part 2
PDF
Perl programming language
ZIP
Round PEG, Round Hole - Parsing Functionally
PDF
Perl.Hacks.On.Vim
Working with text, Regular expressions
Perl Presentation
Class 5 - PHP Strings
php string-part 2
Perl programming language
Round PEG, Round Hole - Parsing Functionally
Perl.Hacks.On.Vim

What's hot (20)

ODP
Introduction to Perl - Day 2
PPTX
PHP Powerpoint -- Teach PHP with this
KEY
Erlang/OTP for Rubyists
PPT
CGI With Object Oriented Perl
PPT
Dealing with Legacy Perl Code - Peter Scott
PDF
Good Evils In Perl
PDF
Perl Scripting
PPTX
String variable in php
PPT
Object Orientation vs. Functional Programming in Python
ODP
Advanced Perl Techniques
PDF
Ruby 2.0
KEY
Introduction to Perl Best Practices
PDF
Perl 5.10 for People Who Aren't Totally Insane
PDF
Zend Certification Preparation Tutorial
ODP
Introducing Modern Perl
ODP
Introduction to Perl - Day 1
Introduction to Perl - Day 2
PHP Powerpoint -- Teach PHP with this
Erlang/OTP for Rubyists
CGI With Object Oriented Perl
Dealing with Legacy Perl Code - Peter Scott
Good Evils In Perl
Perl Scripting
String variable in php
Object Orientation vs. Functional Programming in Python
Advanced Perl Techniques
Ruby 2.0
Introduction to Perl Best Practices
Perl 5.10 for People Who Aren't Totally Insane
Zend Certification Preparation Tutorial
Introducing Modern Perl
Introduction to Perl - Day 1
Ad

Similar to Perl Xpath Lightning Talk (20)

PPT
spug_2008-08
ODP
Modern Perl
ODP
Perl Moderno
ODP
Getting groovy (ODP)
ODP
Maybe you do not know that ...
PPTX
Php 2
ODP
Intro to #memtech PHP 2011-12-05
ODP
What's new in Perl 5.10?
PPT
XML and Web Services with PHP5 and PEAR
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Erlang with Regexp Perl And Port
PDF
[Erlang LT] Regexp Perl And Port
PPT
03 Php Array String Functions
PPT
Ods Markup And Tagsets: A Tutorial
ODP
PHP 102: Out with the Bad, In with the Good
PPT
LPW: Beginners Perl
PDF
Petitparser at the Deep into Smalltalk School 2011
ODP
Beginning Perl
spug_2008-08
Modern Perl
Perl Moderno
Getting groovy (ODP)
Maybe you do not know that ...
Php 2
Intro to #memtech PHP 2011-12-05
What's new in Perl 5.10?
XML and Web Services with PHP5 and PEAR
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Erlang with Regexp Perl And Port
[Erlang LT] Regexp Perl And Port
03 Php Array String Functions
Ods Markup And Tagsets: A Tutorial
PHP 102: Out with the Bad, In with the Good
LPW: Beginners Perl
Petitparser at the Deep into Smalltalk School 2011
Beginning Perl
Ad

Recently uploaded (20)

PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
SparkLabs Primer on Artificial Intelligence 2025
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PDF
Doc9.....................................
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
agentic-ai-and-the-future-of-autonomous-systems.pdf
Revolutionize Operations with Intelligent IoT Monitoring and Control
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
madgavkar20181017ppt McKinsey Presentation.pdf
Smarter Business Operations Powered by IoT Remote Monitoring
SparkLabs Primer on Artificial Intelligence 2025
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CroxyProxy Instagram Access id login.pptx
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
Reimagining Insurance: Connected Data for Confident Decisions.pdf
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Transforming Manufacturing operations through Intelligent Integrations
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Automating ArcGIS Content Discovery with FME: A Real World Use Case
creating-agentic-ai-solutions-leveraging-aws.pdf
Doc9.....................................

Perl Xpath Lightning Talk

  • 3. XPATH Terminology Items Nodes Atomic values Relationship of Nodes Parent Children Siblings Ancestors Descendants
  • 4. XPATH Syntax != div - + * >= > = <= < or and | [] @ .. . // / Nodename
  • 5. XML Sample <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Mastering Perl</title> <author>brian d foy</author> <year>2007</year> <price>39.99</price> </node> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Perl Best Practices</title> <author>Damian Conway</author> <year>2005</year> <price>39.95</price> </node> <node subject=&quot;OO&quot;> <title lang=&quot;en&quot;>Design Patterns: Elements of Reusable Object-Oriented Software</title> <author>Erich Gamma</author> <author>Richard Helm</author> <author>Ralph Johnson</author> <author>John Vlissides</author> <year>1994</year> <price>59.99</price> </node> <node subject=&quot;RegEx&quot;> <title lang=&quot;en&quot;>Mastering Regular Expressions , Third Edition</title> <author>Jeffrey E. F. Friedl</author> <year>2006</year> <price>44.99</price> </node> </root>
  • 6. Sample Script use strict; use warnings; use XML::LibXML; # To the daring win32 perl people: use the ppm command line # to install this package, the gui still doesn’t give the right answers # to the setup procedure. #### Fetch libxml2.dll? [yes] yes #### Where should libxml2.dll be placed? [C:\Perl\bin] <ENTER> my $RawXml = '<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> .. </root>'; my $parser = XML::LibXML->new(); my $tree = $parser->parse_string($RawXml); my $root = $tree->getDocumentElement(); .. foreach my $N ( $bookshelf->findnodes(' XPATH_EXPRESSION ') ) { # print $N. &quot;\n&quot;; # XML::LibXML::Element=SCALAR(0x1a7a7d4) print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; }
  • 7. Searching throughout on from the current (xml libxml object) node use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('//title') ) {
  • 8. Searching throughout the specified xml tree path use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('book') ) { foreach my $N ( $bookshelf->findnodes('title') ) { # won't work since no 'title' child objects at this xml tree level foreach my $N ( $bookshelf->findnodes('book/title') ) { foreach my $N ( $bookshelf->findnodes('/bookshelf/book/price') ) {
  • 9. Playing with XPath positions use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('book[1]') ) { foreach my $N ( $bookshelf->findnodes('book[1]/title') ) { foreach my $N ( $bookshelf->findnodes('book[position()<3]') ) { foreach my $N ( $bookshelf->findnodes('book[last()-1]') ) { foreach my $N ( $bookshelf->findnodes('book[last()]') ) {
  • 10. Querying element values use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('/bookshelf/book[price>40 and price<50]/title') ) { foreach my $N ( $bookshelf->findnodes('/bookshelf/book[price>50]/title') ) {
  • 11. Querying attributes use strict; use warnings; use XML::LibXML; .. foreach my $N ( $bookshelf->findnodes('//@lang') ) { foreach my $N ( $bookshelf->findnodes('//title[@id]') ) { foreach my $N ( $bookshelf->findnodes('//title[@lang]') ) { foreach my $N ( $bookshelf->findnodes('//title[@id | @ooo]') ) { foreach my $N ( $bookshelf->findnodes( ' //title[@id= &quot; en &quot; ] ' ) ) {
  • 13. XML Sample <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Mastering Perl</title> <author>brian d foy</author> <year>2007</year> <price>39.99</price> </node> <node subject=&quot;Perl&quot;> <title lang=&quot;en&quot;>Perl Best Practices</title> <author>Damian Conway</author> <year>2005</year> <price>39.95</price> </node> <node subject=&quot;OO&quot;> <title lang=&quot;en&quot;>Design Patterns: Elements of Reusable Object-Oriented Software</title> <author>Erich Gamma</author> <author>Richard Helm</author> <author>Ralph Johnson</author> <author>John Vlissides</author> <year>1994</year> <price>59.99</price> </node> <node subject=&quot;RegEx&quot;> <title lang=&quot;en&quot;>Mastering Regular Expressions , Third Edition</title> <author>Jeffrey E. F. Friedl</author> <year>2006</year> <price>44.99</price> </node> </root>
  • 14. Sample Script header use strict; use warnings; use XML::LibXML; my $RawXml = '<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <root> .. </root>'; my $parser = XML::LibXML->new(); my $tree = $parser->parse_string($RawXml); my $root = $tree->getDocumentElement();
  • 15. Find all nodes ## Find all nodes foreach my $N ( $root->findnodes('node') ) { print $N. &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## Output Sample: ## XML::LibXML::Element=SCALAR(0x1a7a7d4) ## node=&quot; ## Mastering Perl ## brian d foy ## 2007 ## 39.99 ## &quot; ## XML::LibXML::Element=SCALAR(0x1a7a7f4) ## node=&quot; ## Perl Best Practices ## Damian Conway ## 2005 ## 39.95 ## &quot; ## XML::LibXML::Element=SCALAR(0x1a7a814) ## node=&quot; ## Design Patterns: Elements of Reusable Object-Oriented Software ## Erich Gamma ## Richard Helm ## Ralph Johnson ## John Vlissides ## 1994 ## 59.99 ## &quot; ## XML::LibXML::Element=SCALAR(0x1a7a834) ## node=&quot; ## Mastering Regular Expressions , Third Edition ## Jeffrey E. F. Friedl ## 2006 ## 44.99 ## &quot;
  • 16. Find first node ## Find first node foreach my $N ( $root->findnodes('node[1]') ) { print $N->getAttribute(&quot;subject&quot;). &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## Perl ## node=&quot; ## Mastering Perl ## brian d foy ## 2007 ## 39.99 ## &quot;
  • 17. Find last node ## Find last node foreach my $N ( $root->findnodes('node[last()]') ) { print $N->getAttribute(&quot;subject&quot;). &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## RegEx ## node=&quot; ## Mastering Regular Expressions , Third Edition ## Jeffrey E. F. Friedl ## 2006 ## 44.99 ## &quot;
  • 18. Find last-1 node ## Find last-1 node foreach my $N ( $root->findnodes('node[last()-1]') ) { print $N->getAttribute(&quot;subject&quot;). &quot;\n&quot;; print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## OO ## node=&quot; ## Design Patterns: Elements of Reusable Object-Oriented Software ## Erich Gamma ## Richard Helm ## Ralph Johnson ## John Vlissides ## 1994 ## 59.99 ## &quot;
  • 19. Find all titles ## Find all titles foreach my $N ( $root->findnodes('//title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Mastering Perl&quot; ## title=&quot;Perl Best Practices&quot; ## title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot; ## title=&quot;Mastering Regular Expressions , Third Edition&quot; ## title=&quot;Perl Best Practices duplicate&quot;
  • 20. Find all node/titles ## Find all node/titles foreach my $N ( $root->findnodes('node/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Mastering Perl&quot; ## title=&quot;Perl Best Practices&quot; ## title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot; ## title=&quot;Mastering Regular Expressions , Third Edition&quot;
  • 21. Find all nodes/prices/text ## Find all nodes/prices foreach my $N ( $root->findnodes('/root/node/price/text()') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## #text=&quot;39.99&quot; ## #text=&quot;39.95&quot; ## #text=&quot;59.99&quot; ## #text=&quot;44.99&quot;
  • 22. Find all nodes/prices ## Find all nodes/prices foreach my $N ( $root->findnodes('/root/node/price') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## price=&quot;39.99&quot; ## price=&quot;39.95&quot; ## price=&quot;59.99&quot; ## price=&quot;44.99&quot;
  • 23. Find first node/title ## Find first node/title foreach my $N ( $root->findnodes('node[1]/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Mastering Perl&quot;
  • 24. Find all nodes/titles where price>50 ## Find all nodes/titles where price>50 foreach my $N ( $root->findnodes('/root/node[price>50]/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot;
  • 25. Find all nodes/titles where price<50 and price>40 ## Find all nodes/titles where price<50 and price>40 foreach my $N ( $root->findnodes('/root/node[price>40 and price<50]/title') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Mastering Regular Expressions , Third Edition“ ## title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot;
  • 26. Find all values for the attribute 'lang' ## Find all values for the attribute 'lang' foreach my $N ( $root->findnodes('//@lang') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## lang=&quot;en&quot; ## lang=&quot;en&quot; ## lang=&quot;en&quot; ## lang=&quot;en&quot;
  • 27. Find all titles with the attribute 'lang' ## Find all titles with the attribute 'lang' foreach my $N ( $root->findnodes('//title[@lang]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Mastering Perl&quot; ## title=&quot;Perl Best Practices&quot; ## title=&quot;Design Patterns: Elements of Reusable Object-Oriented Software&quot; ## title=&quot;Mastering Regular Expressions , Third Edition&quot;
  • 28. Find all titles with the attribute 'id' ## Find all titles with the attribute 'id' foreach my $N ( $root->findnodes('//title[@id]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Perl Best Practices duplicate&quot;
  • 29. Find all titles with the attribute 'id=&quot;en&quot;' foreach my $N ( $root->findnodes('//title[@id=&quot;en&quot;]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Perl Best Practices duplicate&quot;
  • 30. Find all titles with the attribute 'id' or with the attribute 'ooo' ## Find all titles with the attribute 'id' or with the attribute 'ooo' foreach my $N ( $root->findnodes('//title[@id | @ooo]') ) { print $N->getName() . &quot;=&quot; . &quot;\&quot;&quot; . $N->textContent() . &quot;\&quot;&quot; . &quot;\n&quot;; } ## title=&quot;Perl Best Practices duplicate&quot;

Editor's Notes

  • #3: What is XPath? XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in several XML Based Technologies
  • #4: Nodes In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node). Atomic values Atomic values are nodes with no children or parent. Items Items are atomic values or nodes. Relationship of Nodes Parent Each element and attribute has one parent. Children Element nodes may have zero, one or more children. Siblings Nodes that have the same parent. Ancestors A node&apos;s parent, parent&apos;s parent, etc. Descendants A node&apos;s children, children&apos;s children, etc.
  • #5: Selecting Nodes XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below: Expression Description nodename Selects all child nodes of the named node / Selects from the root node // Selects nodes in the document from the current node that match the selection no matter where they are . Selects the current node .. Selects the parent of the current node @ Selects attributes Wildcard Description Matches any element node Selecting Several Paths By using the | operator in an XPath expression you can select several paths.