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


In reply to Perl LibXML XPATH query issues. by DunLidjun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.