Wednesday, December 29, 2010

Using XML::XPath to get nodes and Strings

#Xpath.pl data1.xml "/inventory/category/item/name"
#Xpath.pl data1.xml "/inventory/category/item/name[@style='latin']"
#Xpath.pl data1.xml "//item[@id='222']/note"
#Xpath.pl data1.xml "//item[@id='222']/note/text( )"
#Xpath.pl data1.xml "/inventory/@date"
#Xpath.pl data1.xml "//*[@id='104']/parent::*/preceding-sibling::*/child::*[2]/name[not(@style='latin')]/node( )"


use XML::XPath;
use XML::XPath::XMLParser;

# create an object to parse the file and field XPath queries
my $xpath = XML::XPath->new( filename => shift @ARGV );

# apply the path from the command line and get back a list matches
my $nodeset = $xpath->find( shift @ARGV );

# print each node in the list
foreach my $node ( $nodeset->get_nodelist ) {
  print XML::XPath::XMLParser::as_string( $node ) . "\n";
}

No comments: