title: Validate XML using LibXML
body: <p>Currently, I am using the XML::LibXML perl module to validate an XML file against a defined XML schema. At the moment, if my XML file fails to validate successfully against the defined XML Schema, I will get a list of errors informing me, for example that certain elements were not expected and then what was expected instead. In my XML file I will have many elements of the same name but they may be nested in various places in the XML file.</p>
  
  <p>My question is, is there anyway in which I can output the XPath location of any elements that may error when attempting to perform the validation?</p>
  
  <p>Currently, my XML file is quite big and it is hard to "debug" it when validation fails as the name of the element that is displayed in the error, may occur many times in various places in the XML file.</p>
  
  <p>My code is below for using LibXML to validate an XML file against a schema.</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  use XML::LibXML;
  
  my $schema_file = 'MySchema.xml';
  my $document    = 'MyFile.xml';
  
  my $schema = XML::LibXML::Schema-&gt;new(location =&gt; $schema_file);
  
  my $parser = XML::LibXML-&gt;new;
  my $doc    = $parser-&gt;parse_file($document);
  
  eval { $schema-&gt;validate($doc) };
  die $@ if $@;
  
  print "$document validated successfully\n";
  </code></pre>