tomhukins has asked for the wisdom of the Perl Monks concerning the following question:
I want to use XML::LibXML to process OpenOffice (sxw) files. OpenOffice stores its information in a Zip archive containing several XML files. I have extracted the content.xml file from such an archive.
I have used XML::LibXML before, but never with XML documents that use namespaces. OpenOffice files use various namespaces for the different types of content.
Normally, I would write something like:
to retrieve all the nodes of type p. OpenOffice stores paragraphs in p elements within the text namespace. So, I replaced the findnodes line above with:my $parser = XML::LibXML->new() or die $!; my $tree = $parser->parse_file('content.xml') or die $!; my @nodes = $tree->findnodes('//p');
but this returns the error XPath error Undefined namespace prefix in //text:p xmlXPathCompiledEval: evaluation failed.my @nodes = $tree->findnodes('//text:p');
I fixed this problem with:
but I don't understand why one way works and the other way doesn't. Both contexts (tree and documentElement) work with another XML document that does not use namespaces.my @nodes = $tree->documentElement->findnodes('//text:p');
Can anyone here enlighten me?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::LibXML and XML Namespaces (processing OpenOffice documents)
by dakkar (Hermit) on Mar 11, 2003 at 15:06 UTC | |
by bart (Canon) on Mar 11, 2003 at 19:34 UTC | |
by IlyaM (Parson) on Mar 11, 2003 at 21:48 UTC | |
|
Re: XML::LibXML and XML Namespaces (processing OpenOffice documents)
by bronto (Priest) on Mar 11, 2003 at 14:31 UTC |