[XML4Lib] argg, namespaces
Eric Lease Morgan
emorgan at nd.edu
Sat Jul 14 13:39:55 EDT 2007
Argg! Namespaces, a necessary evil.
Seriously, how to I write a set of XPath statments to parse an XML
file that contains a namespace?
I have the following MODS file, and it includes an un-prefixed
namespace (http://www.loc.gov/mods/v3):
<?xml version="1.0" encoding="UTF-8"?>
<modsCollection xmlns:xsi = "http://www.w3.org/2001/XMLSchema-
instance"
xmlns = "http://www.loc.gov/mods/v3"
xsi:schemaLocation = "http://www.loc.gov/mods/v3
http://www.loc.gov/standards/mods/v3/mods-3-0.xsd">
<mods version="3.0">
<titleInfo>
<title>[Adarna children's book publications]</title>
</titleInfo>
<!-- more great mods content here -->
</mods>
</modsCollection>
Then, using Perl, I create a LibXML parser, parse the file ($input)
creating an object, and try to loop through all of the mods elements:
$parser = XML::LibXML->new;
$collection = $parser->parse_file( $input );
foreach my $mods ( $collection->findnodes( '//mods' )) {
my $titles = '';
foreach $node ( $mods->findnodes( './/titleInfo/title' )) {
$titles .= $node->textContent . '|'
}
# do more cool stuff here
}
As written my Perl script never enters the foreach loop, but as soon
as I remove the namespace declaration from the MODS file the script
works just fine.
How do I specify the namespace in my findnodes method?
--
Eric Morgan
More information about the XML4Lib
mailing list