The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

PPIx::XPath - an XPath implementation for the PDOM

SYNOPSIS

  use PPI;
  use PPI::XPath;
  use Tree::XPathEngine;

  my $pdom = PPI::Document->new('some_code.pl');
  my $xpath = Tree::XPathEngine->new();
  my @subs = $xpath->findnodes('//Statement-Sub',$pdom);
  my @vars = $xpath->findnodes('//Token-Symbol',$pdom);

Deprecated interface, backward-compatible with PPIx::XPath version 1:

  use PPIx::XPath;

  my $pxp  = PPIx::XPath->new("some_code.pl");
  my @subs = $pxp->match("//Statement::Sub");
  my $vars = $pxp->match("//Token::Symbol");

DESCRIPTION

This module augments PPI's classes with the methods required by Tree::XPathEngine, allowing you to perform complex XPath matches against any PDOM tree.

See Tree::XPathEngine for details about its methods.

Mapping the PDOM to the XPath data model

  • Each node in the PDOM is an element as seen by XPath

  • The name of the element is the class name of the node, minus the initial PPI::, with :: replaced by -. That is:

      ($xpath_name = substr($pdom_node->class,5)) =~ s/::/-/g;
  • Only "significant" nodes are seen by XPath

  • all scalar-valued accessors of PDOM nodes are visible as attributes

  • "here-docs" contents are not mapped

BUGS and LIMITATIONS

  • "here-docs" contents are not mapped

  • node ordering is slow, because I could not find a way in PPI to compare two nodes for document order; suggestions are most welcome

SEE ALSO

PPI

Tree::XPathEngine

http://www.w3.org/TR/xpath (the XPath specification)

AUTHOR

Dan Brook <cpan@broquaint.com> original author

Gianni Ceccarelli <dakkar@thenautilus.net> Tree::XPathEngine-based re-implementation