in reply to XML::LibXML parentNode only root element
Or, keeping the xpath expression simple, plus testing element after breakfast menu:
#!/usr/bin/env perl use strict; use warnings; use XML::LibXML; my $dom = XML::LibXML->load_xml(string => <<'EOT'); <?xml version="1.0" encoding="UTF-8"?> <restaurant> <timestart>0600</timestart> <timeend>1030</timeend> <city>Boston</city> <country>US</country> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>Two of ...</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>Light Belgian ...</description> <calories>900</calories> </food> </breakfast_menu> <edgecase>test after breakfast</edgecase> </restaurant> EOT my $root = $dom->getDocumentElement(); for ($root->findnodes('/restaurant/*')) { next if 0 == index($_, '<breakfast_menu>'); print "$_\n"; }
Output:
<timestart>0600</timestart> <timeend>1030</timeend> <city>Boston</city> <country>US</country> <edgecase>test after breakfast</edgecase>
Note: If you only want elements before breakfast menu, change next to last in the for loop.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::LibXML parentNode only root element
by ikegami (Patriarch) on Apr 25, 2023 at 13:24 UTC |