ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
Re: XMP Parsing Question
by Michael Nachbaur other posts by this author
Sep 22 2005 3:10PM messages near this date
view in the new Beta List Site
Re: XMP Parsing Question | Perl+XML
& XSLT Bruce Miller wrote:
>  Michael Nachbaur wrote:
>  
> > my $parser = new XML::LibXML();
> > my $doc = $parser->parse_string($xmp);
> > # or:
> > # my $doc = $parser->parse_file($xmp_filename);
> > my $rootNode = $doc->documentElement();
> > my $image = $rootNode->findvalue('//xapgimg:image');
> >
> > Or if you want to be more specific, you can use the following for the 
> > last line:
> > my $image = 
> > $rootNode->findvalue('/x:xmpmeta/rdf:rdf//xap:thumbnails//xapgimg:image'); 
> >
> >
> > This code assumes that only one thumbnail exists.  However, it should 
> > suffice.
>  
>  
>  Actually (I think), because there can be multiples, findnode returns
>  a list.  Unless findnode has specially coded it (which I doubt),
>  in a scalar context, it will return the number of nodes found, rather than
>  the node.  So, you'll do better with:
>  
>  my ($firstimage) = $rootNode->findvalue(..xpath...);
>  
>  Incidentally, since the example is heavily using namespaces,
>  it will be safer to use a more convoluted xpath like:
>   //*[local-name()='image' and namespace-url()='whatever']
>  
>  You'll probably be happier in the long run install
>  XML::LibXML::XPathContext and then
>  
>  my $xpcontext = XML::LibXML::XPathContext->new();
>  $xpcontext->registerNs('xapgimp','whatever');
>  my ($firstimage)->findvalue("//xapgimp:image", $rootNode);

Yeah, so the code for something like this would look like:
use XML::LibXML;
use XML::LibXML::XPathContext;

my $xmp = "XML String Here";
my $parser = new XML::LibXML();
my $doc = $parser-> parse_string($xmp);
my $xc = XML::LibXML::XPathContext-> new($doc);
$xc-> registerNs('xapgimg','http://ns.adobe.com/xap/1.0/g/img/');

my $rootNode = $doc-> documentElement();
my $image = $xc-> findvalue('//xapgimg:image');
print "$image\n";


I just tested this on my computer, and it works well.  The problem, it 
seems, is that since the multiple namespaces aren't defined on the root 
XML element, LibXML doesn't know about all of them.  So you have to 
define it yourself.

_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Christopher Pryce
Michael Nachbaur
Christopher Pryce
Bruce Miller
Michael Nachbaur

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved