Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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?

Thanks,

Shawn Way

Comment on Perl LibXML XPATH query issues.
Select or Download Code
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 xpather.pl it can tell you xpaths

Re: Perl LibXML XPATH query issues.
by tangent (Curate) 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.

      Thanks!

      Shawn Way

        At a guess I'd say you had '/states/...' rather than 'states/...'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1072677]
Approved by keszler
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-12-18 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls