title: Perl, LibXML and Schemas
body: <p>I have an example Perl script which I am trying to load and validate a file against a schema, them interrogate various nodes. </p>
  
  <pre><code>#!/usr/bin/env perl
  use strict;
  use warnings;
  use XML::LibXML;
  
  my $filename = 'source.xml';
  my $xml_schema = XML::LibXML::Schema-&gt;new(location=&gt;'library.xsd');
  my $parser = XML::LibXML-&gt;new ();
  my $doc = $parser-&gt;parse_file ($filename);
  
  eval {
      $xml_schema-&gt;validate ($doc);
  };
  
  if ($@) {
      print "File failed validation: $@" if $@;
  }
  
  eval {
      print "Here\n";
      foreach my $book ($doc-&gt;findnodes('/library/book')) {
          my $title = $book-&gt;findnodes('./title');
          print $title-&gt;to_literal(), "\n";
  
      }
  };
  
  if ($@) {
      print "Problem parsing data : $@\n";
  }
  </code></pre>
  
  <p>Unfortunately, although it is validating the XML file fine, it is not finding any $book items and therefore not printing out anything.  </p>
  
  <p>If I remove the schema from the XML file and the validation from the PL file then it works fine.</p>
  
  <p>I am using the default namespace.  If I change it to not use the default namespace (xmlns:lib="http://libs.domain.com" and prefix all items in the XML file with lib and change the XPath expressions to include the namespace prefix (/lib:library/lib:book) then it again works file.</p>
  
  <p>Why? and what am I missing?</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;library xmlns="http://lib.domain.com" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://lib.domain.com .\library.xsd"&gt;
      &lt;book&gt;
          &lt;title&gt;Perl Best Practices&lt;/title&gt;
          &lt;author&gt;Damian Conway&lt;/author&gt;
          &lt;isbn&gt;0596001738&lt;/isbn&gt;
          &lt;pages&gt;542&lt;/pages&gt;
          &lt;image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190"/&gt;
      &lt;/book&gt;
      &lt;book&gt;
          &lt;title&gt;Perl Cookbook, Second Edition&lt;/title&gt;
          &lt;author&gt;Tom Christiansen&lt;/author&gt;
          &lt;author&gt;Nathan Torkington&lt;/author&gt;
          &lt;isbn&gt;0596003137&lt;/isbn&gt;
          &lt;pages&gt;964&lt;/pages&gt;
          &lt;image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif" width="145" height="190"/&gt;
      &lt;/book&gt;
      &lt;book&gt;
          &lt;title&gt;Guitar for Dummies&lt;/title&gt;
          &lt;author&gt;Mark Phillips&lt;/author&gt;
          &lt;author&gt;John Chappell&lt;/author&gt;
          &lt;isbn&gt;076455106X&lt;/isbn&gt;
          &lt;pages&gt;392&lt;/pages&gt;
          &lt;image src="http://media.wiley.com/product_data/coverImage/6X/07645510/076455106X.jpg" width="100" height="125"/&gt;
      &lt;/book&gt;
  &lt;/library&gt;
  </code></pre>
  
  <p>XSD:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema xmlns="http://lib.domain.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://lib.domain.com"&gt;
      &lt;xs:attributeGroup name="imagegroup"&gt;
          &lt;xs:attribute name="src" type="xs:string"/&gt;
          &lt;xs:attribute name="width" type="xs:integer"/&gt;
          &lt;xs:attribute name="height" type="xs:integer"/&gt;
      &lt;/xs:attributeGroup&gt;
      &lt;xs:element name="library"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element maxOccurs="unbounded" name="book"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element name="title" type="xs:string"/&gt;
                              &lt;xs:element maxOccurs="unbounded" name="author" type="xs:string"/&gt;
                              &lt;xs:element name="isbn" type="xs:string"/&gt;
                              &lt;xs:element name="pages" type="xs:integer"/&gt;
                              &lt;xs:element name="image"&gt;
                                  &lt;xs:complexType&gt;
                                      &lt;xs:attributeGroup ref="imagegroup"/&gt;
                                  &lt;/xs:complexType&gt;
                              &lt;/xs:element&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>