jswamin has asked for the wisdom of the Perl Monks concerning the following question:

I'm scratching my head over this and hope you can point out the mistake I'm making here. Essentially I'm trying to parse a very large XML file (~600Mb) chunk-by-chunk using XML::Twig and trying to extract relevant information. I find that I cannot get value of an attribute inside an element using findvalue. XML snippet and code below. Thanks for your help!

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ReleaseSet Dated="2014-02-11" Type="full"> <ClinVarSet ID="176987"> <RecordStatus>current</RecordStatus> <Title>Single allele AND Benign familial neonatal seizures 1</Title> <ReferenceClinVarAssertion> <ClinVarAccession Acc="RCV000020962" Version="1" Type="RCV" DateUp +dated="2013-05-06"/> </ReferenceClinVarAssertion> </ClinVarSet> </ReleaseSet>

And the perl bit

#!/usr/bin/perl use strict; use warnings; use XML::Twig; use Data::Dumper; my $twig= XML::Twig::XPath->new( twig_handlers => { ClinVarSet => \&ClinVar }, pretty_print => 'indented' ); $twig->parsefile($file); $twig->dispose; sub ClinVar { my( $twig, $Clin)= @_; my $ClinVarSetID = $Clin->{'att'}->{'ID'}; my $status = $Clin->first_child('RecordStatus')->text; my $Accession = $Clin->findvalue('./ReferenceClinVarAssertion/ClinVa +rAccession[@Acc]'); print "ID: $ClinVarSetID\tStatus: $status\tAccession: $Accession\n"; }

I get the first two values $ClinVarSetID and $status but I cannot get the value of the attribute 'Acc' using the XPath

Replies are listed 'Best First'.
Re: Using findvalue in XML::Twig::XPath
by tangent (Parson) on Feb 26, 2014 at 22:27 UTC
    This works for me:
    my $Accession = $Clin->findvalue('./ReferenceClinVarAssertion/ClinVarA +ccession/@Acc');
Re: Using findvalue in XML::Twig::XPath ( xpath is xpath )
by Anonymous Monk on Feb 27, 2014 at 01:11 UTC

    You need to brush up on your xpath, the element node you're selecting ( ClinVarAccession with an @Acc attribute ) has no children or content -- it has no value

    If you want the value of the @Acc attribute do like tangent shows in Re: Using findvalue in XML::Twig::XPath, select the attribute

      Oh dear me!! Of course... Thanks for your help!