Por que não posso acessar elementos dentro de um arquivo XML com XPath em XML :: libxml?

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

Pergunta

Eu tenho um arquivo XML, parte do qual se parece com o seguinte:

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

Estou tentando analisar o arquivo com o Libxml da Perl e a saída do Wellname e o RawData usando o seguinte:

    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;
        }

Não está imprimindo nada agora e acho que tenho um problema com minhas declarações XPath. Por favor, você pode me ajudar a consertar, ou pode me mostrar como soltar problemas das declarações XPath? Obrigado.

Foi útil?

Solução

Ao invés de usar findnodes no loop, use getElementsByTagName ():

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

Aqui estão alguns outros métodos úteis para usar o processamento para @c variedade:

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

Outras dicas

Se o elemento 'onda' estiver em um espaço de nome, o elemento 'RawData' também é bem, então você provavelmente precisa usar

foreach $nod (@n) {
    my @c = $xc->findnodes('descendant::ns:rawData', $nod);  #element inside the tree.
    print @c;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top