Pourquoi ne puis-je accéder à des éléments à l'intérieur d'un fichier XML avec XPath en XML :: LibXML?

StackOverflow https://stackoverflow.com/questions/2079668

Question

J'ai un fichier XML, dont une partie ressemble à ceci:

 <wave waveID="1">
    <well wellID="1" wellName="A1">
      <oneDataSet>
        <rawData>0.1123975676</rawData>
      </oneDataSet>
    </well>
    ... more wellID's and rawData continues here...

Je suis en train d'analyser le fichier avec Libxml de Perl et de sortie de la wellName et l'rawData en utilisant les éléments suivants:

    use XML::LibXML;
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file('/Users/johncumbers/Temp/1_12-18-09-111823.orig.xml');
    my $xc = XML::LibXML::XPathContext->new( $doc->documentElement()  );
    $xc->registerNs('ns', 'http://moleculardevices.com/microplateML');

            my @n = $xc->findnodes('//ns:wave[@waveID="1"]');   #xc is xpathContent
        # should find a tree from the node representing everything beneath the waveID 1
        foreach $nod (@n) {
            my @c = $nod->findnodes('//rawData');  #element inside the tree.
            print @c;
        }

Il n'imprime pas quoi que ce soit en ce moment et je pense avoir un problème avec mes déclarations XPath. S'il vous plaît pouvez-vous me aider à résoudre, ou pouvez-vous me montrer comment tirer sur les déclarations du mal de XPath? Merci.

Était-ce utile?

La solution

Au lieu d'utiliser findnodes dans la boucle, utilisez getElementsByTagName () :

my @c = $nod->getElementsByTagName('rawData');

Voici quelques autres méthodes pratiques pour utiliser le traitement à la matrice @c:

$c[0]->toString;    # <rawData>0.1123975676</rawData>
$c[0]->nodeName;    # rawData
$c[0]->textContent; # 0.1123975676

Autres conseils

Si l'élément « vague » est dans un espace de nom alors l'élément « rawData » est aussi bien que vous devez probablement utiliser

foreach $nod (@n) {
    my @c = $xc->findnodes('descendant::ns:rawData', $nod);  #element inside the tree.
    print @c;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top