Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hello everybody, i think i have a simple question but i can't find an aswer to it anywhere so i hope you can help.
I have an xml file with namespace and i want to select a node with an attribute with a specific value. Here is the xml file:

<?xml version="1.0" encoding="UTF-8"?> <guestbook xmlns="http://myweb.com" xmlns:xs="http://www.w3.org/2001/X +MLSchema-instance" nome="740" xs:schemaLocation="http://myweb.com gue +stbook.xsd"> <comment confermato="true" timestamp="20130627112554"> <guest>...</guest> </comment> <comment confermato="false" timestamp="20130903225513"> <guest>...</guest> </comment> <comment confermato="false" timestamp="20130904191337"> <guest>...</guest> </comment> </guestbook>

I need, with a cgi script, to select the node "comment" with the timestamp "20130903225513". The problem is that i don't know hot to put the namespace correctly. Here is the code for the script

#!/usr/bin/perl use strict; use XML::LibXML; use HTML::Entities; use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use Fcntl qw(:flock); my $cgi = new CGI; print "Content-type: text/html\n\n"; my $file_dati = "../data/xml/guestbook_xml.xml"; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($file_dati) || die("dead1"); my $root = $doc->getDocumentElement || die("dead2"); my $nodetobefound= "xs:commento[xs:\@timestamp='20130903225513']"; print "$nodetobefound\n"; my $commento = $root->findnodes("$nodetobefound") || die("dead3"); print $commento->get_node(0)->toString();

If i don't insert any namespace in the query i finish in the "dead3" die, if i put any kind of namespace i receive an "Xpath error: invalid expression".
Any hint on how to write the correct xpath query?
thanx in advance, Giulia


In reply to How to write an xpath query including attributes for an XML with namespace by topola1882

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (9)
    As of 2014-07-30 23:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (241 votes), past polls