[MOBY-dev] Perl question
Andreas Groscurth
groscurt at mpiz-koeln.mpg.de
Fri Jun 8 12:21:04 UTC 2007
Hiho,
because of the awesome perl hackers on the list.. I hope someone can help
me....
I have the following Moby response
<?xml version="1.0" encoding="UTF-8"?>
<moby:MOBY xmlns:moby="http://www.biomoby.org/moby">
<moby:mobyContent>
<moby:mobyData moby:queryID="sip_1_">
<moby:Collection moby:articleName="pubmedID">
<moby:Simple>
<moby:Object moby:id="" moby:namespace="PMID_" />
</moby:Simple>
<moby:Simple>
<moby:Object moby:id="7743940 " moby:namespace="PMID_" />
</moby:Simple>
<moby:Simple>
<moby:Object moby:id="9090879 " moby:namespace="PMID_" />
</moby:Simple>
<moby:Simple>
<moby:Object moby:id="9090880 " moby:namespace="PMID_" />
</moby:Simple>
</moby:Collection>
</moby:mobyData>
</moby:mobyContent>
</moby:MOBY>
and the following perl code
my $parser = XML::LibXML->new();
$parser->clean_namespaces( 1 );
my $doc = $parser->parse_string($resultXML);
my $root = $doc->getDocumentElement;
my @objectNodes = ();
# $outObjType = Object (!)
@objectNodes = $root->findnodes("//moby:$outObjType");
# deal with 'moby-less' xml tags:
@objectNodes = $root->findnodes("//$outObjType") unless @objectNodes;
my $resCnt = 0; # a result counter, used as default for file naming
# iterate over all result objects
foreach my $objectNode (@objectNodes) {
# create a filename and write the object to the cache directory
# if provider uses other namespaces (that is other
# than the start namespace of the application), get
# this and use it for cache file name
my ($ns,$id);
if ( $objectNode->hasAttributeNS('moby', 'namespace') ) {
$ns = $objectNode->getAttributeNS('moby', 'namespace');
}
# deal with 'moby-less' attributes
elsif ( $objectNode->hasAttribute('namespace') ) {
$ns = $objectNode->getAttribute('namespace');
}
as far as I understand this code (is from Dirks aggregator), it should parse
the namespace of the element 'moby:Object'... but it doesnt.
I know nothing about LibXML and parsing xml in Perl.. so I was wondering, why
if ( $objectNode->hasAttributeNS('moby', 'namespace') ) {
$ns = $objectNode->getAttributeNS('moby', 'namespace');
}
is not working.
Any hints ?
Thanks
Andreas
More information about the MOBY-dev
mailing list