in reply to XPATH as_string losing prolog

The XML::XPath::XMLParser::as_string treats all nodes the same, even if you're working with the root node. Therefore it does not contain the header information of a full xml document.

If you want the header, just capture it independently and print it out.

use XML::XPath; use strict; my $filename = 'test.xml'; my $header = do { open my $fh, $filename or die "$filename: $!"; <$fh>; }; my $xp = XML::XPath->new(filename => $filename); $xp->setNodeText("/project/dependencies/dependency[version='1.0']/vers +ion", 2.0); open my $fh, '>', $filename or die "$filename: $!"; print $fh $header; print $fh XML::XPath::XMLParser::as_string($xp->findnodes('/')); close $fh;
Also, use <code> instead of <pre> tags per How do I post a question effectively?.