in reply to perl & XML: getting last child?

Thanks for the help on the last child... Related question for situations where there is no value in the node but i need to return a "0" to maintain the appropriate output file structure. Modifying the original files slightly below:
xml <library> <cd> <artist></artist> </cd> <book> <title>Perl Best Practices</title> <author>Damian Conway</author> <isbn>0596001738</isbn> <pages>542</pages> <image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190" /> </book> <book> <title>Perl Cookbook, Second Edition</title> <author>Tom Christiansen</author> <author>Nathan Torkington</author> <isbn>0596003137</isbn> <pages>964</pages> <image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gi +f" width="145" height="190" /> </book> <book> <title>Guitar</title> <author>Mark Phillips</author> <author>John Chappell</author> <isbn>076455106X</isbn> <pages>392</pages> <image src="http://media.wiley.com/product_data/coverImage/6X/07 +645510/076455106X.jpg" width="100" height="125" /> </book> </library>
Incorporating some of the previous suggestions, new code looks like this. Problem arises when <artist> is null (or there is no entry).
#!/usr/bin/perl -w use strict; use warnings; my $filename = 'library1.xml'; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); print $_->data . " | " foreach ($doc->findnodes('/library/cd/artis +t/text()')); my @nodes = $doc->findnodes('/library/book[last()]'); foreach my $node (@nodes) { print $node->findvalue( 'title')." | \n"; }

Replies are listed 'Best First'.
Re^2: perl & XML: getting last child?
by Lotus1 (Vicar) on Apr 19, 2013 at 01:27 UTC

    You can test what is returned from the text node. There is an xpath expression to do this also but I usually just test it like this. (untested code)

    foreach ($doc->findnodes('/library/cd/artist/text()'){ my $artist = $_->data; if ($artist){ print "$artist | "; } else { print "0 | "; } }

    If the artist was formerly called Prince I'm not sure whether Perl will evaluate that symbol to true or false, (or male or female)... it's a little confusing.

    Update: The above won't work since findnodes() will only return the nodes that have text. I often have the situation where I need to add text to empty text nodes. Here is something closer.

    foreach ($doc->findnodes('/library/cd/artist')){ #update: added ')' my $artist = $_->to_literal; if ($artist){ print "$artist | "; #to change the text use my $textnode=$_->findnodes('./text()') # then use $textnode->setData('new text'); } else { print "0 | "; # you can use $_->appendTextNode('text to add') here } }

    Another option is to loop through seperately the nodes with no text using the not() funtion of xpath.

    foreach ($doc->findnodes('/library/cd/artist[not(text())]'){
      I cannot get your code to work. Any ideas? thanks

        Typically I look at the warnings and errors and fix any problems until the code will run. Then I look at the output to see if I'm getting the expected results and make changes and read the documentation for any modules or functions that aren't working like I expect. I also put in extra print statements to test my assumptions of how the code should work.

        The code I posted has a missing parenthesis but otherwise does what you requested. The second warning message points you to the exact line where this occurs.

        syntax error at C:\b\perlmonks\xml\1029217.pl line __, near "){"