Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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 monks are getting baked in the sun...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2016-12-03 21:07 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (60 votes). Check out past polls.