dave_yankee has asked for the wisdom of the Perl Monks concerning the following question:

I'm able to validate xml using XML::LibXML and XML::LibXML::Dtd, but I need to know which nodes are valid. Is there a way to get a valid node list, or even an xml document showing just the valid nodes? I'd even settle for knowing the invalids and working back from that. Thanks!
  • Comment on Need a way to figure which xml nodes are valid

Replies are listed 'Best First'.
Re: Need a way to figure which xml nodes are valid
by Khen1950fx (Canon) on May 18, 2010 at 00:20 UTC
    I'd start by running a few tests. I based this script on Simple XML Validation with Perl:
    #!/usr/bin/perl use strict; use warnings; use Test; BEGIN { plan tests => 4 } use XML::XPath; ok(1); my $xp = XML::XPath->new(ioref => *DATA); my @nodes; @nodes = $xp->findnodes('/order'); ok @nodes == 1, 1, q[the root element must be an order]; @nodes = $xp->findnodes('/order/customer'); ok @nodes == 1,1, q[an order must contain a customer element]; @nodes = $xp->findnodes('/order/customer/name'); ok @nodes == 1,1, q[an order must contain a customer element with name +]; __DATA__ <?xml version="1.0" standalone="yes"?> <order> <customer> <name>Coyote, Ltd.</name> <shipping_info> <address>1313 Desert Road</address> <city>Nowheresville</city> <state>AZ</state> <zip>90210</zip> </shipping_info> </customer>