Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Perl LibXML XPATH query issues.

by DunLidjun (Acolyte)
on Jan 30, 2014 at 13:50 UTC ( #1072677=perlquestion: print w/replies, xml ) Need Help??
DunLidjun has asked for the wisdom of the Perl Monks concerning the following question:

I'm using LibXML to retreive some data from an XML file, but I'm having issues getting the XPATH query to work.

I have the following data set:

<gfx> <maintainedButton name="MaintainedPushButton1" > <states> <state stateId="Error" > <caption caption="Error"/> <imageSettings imageName="" /> </state> <state stateId="0" value="0" > <caption caption="AUTO PRESS FOR MANUAL"/> <imageSettings imageName="" /> </state> <state stateId="1" value="1"> <caption caption="MANUAL PRESS FOR AUTO" /> <imageSettings imageName=""/> </state> </states> <animations> <animateVisibility expression="({[MECO9434]ENGINEER_SECURI +TY})&#xA;OR (({[MECO9434]OPERATOR_SECURITY}==1) AND ({::[MECO9434]Pro +gram:MECO9432.OFF_A}==1))"/> </animations> <connections> <connection name="Value" expression="{::[MECO9434]Program: +MECO9432.LIC_20_A_M_OIT}"/> <connection name="Indicator" expression="{::[MECO9434]Prog +ram:MECO9432.LIC_20_A_M_OIT}"/> </connections> </maintainedButton> </gfx>

I've sucessfully used the following code to get the button name, but it breaks down when trying to get information about the state.

#!/usr/bin/perl # use strict; use warnings; use threads; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("file.xml"); foreach my $title ($doc->findnodes('//maintainedButton')) { my $button = $title->findvalue('@name'); $button =~ s/\n/ /; my $state = $title->findvalue('/states/state[@stateId ="0"]/@stateId +'); print "$button\t$state\n"; }

What I'm looking to get is:

MaintainedPushButton1 AUTO PRESS FOR MANUAL

Can someone point out what's wrong with the XPATH query and point me to a good reference so I don't have to keep asking these simple questions?


Shawn Way

Replies are listed 'Best First'.
Re: Perl LibXML XPATH query issues.
by tangent (Priest) on Jan 30, 2014 at 14:52 UTC
    This should work:
    my $state = $title->findvalue('states/state[@stateId="0"]/caption/@cap +tion');

      I could have sworn that I used this and it didn't work, but it works now. I don't know what I did wrong.


      Shawn Way

        At a guess I'd say you had '/states/...' rather than 'states/...'
Re: Perl LibXML XPATH query issues.
by Anonymous Monk on Jan 30, 2014 at 14:47 UTC

    try //@stateId and see what you get

    Then ask yourself why you added /@stateId to begin with

    Grab a copy of it can tell you xpaths

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1072677]
Approved by keszler
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2017-03-26 19:21 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.