I am trying to extract some information from xml report generated by a trouble ticket system.

I have an "incident" number, and want to extract additional information from that node.

The script looks like this:

#!/usr/bin/perl use strict; use warnings; use XML::LibXML; use XML::LibXML::XPathContext; my $file = "./incident.xml"; my $parser = XML::LibXML->new(XML_LIBXML_RECOVER => 2); $parser->recover_silently(1); my $dom = $parser->load_xml( location => $file ); my $root = $dom->getDocumentElement(); my $xpc = XML::LibXML::XPathContext->new($root); ############################################## # If we have an incident ticket reference get info from that ticket. my $inc_node = $xpc->findnodes('/upload/incident[number/text() = "INC9 +02531"]'); my $service_restoration_time = $xpc->findvalue( "./u_service_restorati +on_time", $inc_node ); my $assignment_group = $xpc->findvalue( ".//assignment_group/\@display +_value", $inc_node ); # etc... print $service_restoration_time . "\n" ; print $assignment_group . "\n" ;

The XML file that the script parses looks like this (shortened, has thousands of incident entries. There is no namespace declared, not sure how to handle that...

I'm getting "XPathContext: lost current node at ./query_test.pl line 23" as the error.

<?xml version="1.0" encoding="UTF-8"?> <unload unload_date="2010-03-17 13:02:24"> <incident action="INSERT_OR_UPDATE"> <opened_at>2009-12-15 14:29:55</opened_at> <closed_at>2009-12-21 05:00:21</closed_at> <u_problem_summary/> <u_item>none</u_item> <business_duration>1970-01-03 05:30:05</business_duration> <category>none</category> <number>INC902550</number> <u_caller_location/> <u_service_restoration_time>2009-12-15 23:01:17</u_service_restoration +_time> <u_close_code>Service Restored</u_close_code> <u_service_loss_time>2009-12-15 14:29:55</u_service_loss_time> <u_user_issue>Software Issue/Bug</u_user_issue> <element_id>92becbd70a0a140b0187537b8516f18c</element_id> </unload>

Would greatly appreciate being pointed in the right direction!


In reply to XML parsing help need, please... by spstansbury

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.