So, I tried to use a variable to return the values from the XPath, and it's not liking it.. I get an invalid query error. Here is the subroutine I'm using. I must be missing something. I added a few statements to help me see what was happening. The goal is for the XPaths to come from a configuration file (as there are about a dozen, and there are other formats that will be added in the future so there may be hundreds ultimatley) rather than hard coded, but for testing and development I'm doing it this way.

$twig = XML::Twig::XPath->new(); $twig->parsefile($xmlfile); $root = $twig->root; $XPV='//Party[@id=//Relation[child::RelationRoleCode[@tc="37"]]/@Relat +edObjectID]/Producer/CarrierAppointment/CompanyProducerID'; my $retval1 = get_xpath_value($XPV); $XPV='//OLifE/Party[@id=//OLifE/Relation[RelationRoleCode@tc=8]/@Relat +edObjectID]/Person/FirstName'; my $retval2 = get_xpath_value($XPV); print "CompanyProducerID: $retval1 \nFirstName: $retval2\n"; sub get_xpath_value { my ($argvalx) = @_; print "argvalx: $argvalx\n"; my $xpath = "q!" . $argvalx . "!"; print "doing XPATH query on $xpath\n"; my @nodes = $root->findnodes($xpath); for (@nodes){ print $_->text; return $_->text; } } ./twig4.pl -xmlfile=a2b9f375-51fe-41a1-86ab-069561517890.xml argvalx: //Party[@id=//Relation[child::RelationRoleCode[@tc="37"]]/@Re +latedObjectID]/Producer/CarrierAppointment/CompanyProducerID doing XPATH query on q!//Party[@id=//Relation[child::RelationRoleCode[ +@tc="37"]]/@RelatedObjectID]/Producer/CarrierAppointment/CompanyProdu +cerID! Query: q!//Party... ^^^ Invalid query somewhere around here (I think)

In reply to Re^6: Xpath value query by SDwerner
in thread Xpath value query by Anonymous Monk

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.