tobyink provides another solution here Re: LibXML, XPath and Namespaces
my $string = q| <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos +="http://xml.juniper.net/junos/12.3R9/junos"> <vlan-information junos:style="terse"> <vlan> <vlan-name>Vlan</vlan-name> </vlan> </vlan-information> </rpc-reply>|; my $jparser = XML::LibXML->new; my $doc = $jparser->parse_string($string); my @vlans = $doc->findnodes("//vlan-information/vlan"); print "found ", scalar @vlans, " vlans\n"; print "The root element's namespace is: ", $doc->documentElement->namespaceURI, "\n"; # Give that namespace a prefix so that we can reference it in XPath $doc->documentElement->setNamespaceDeclPrefix("", "x"); @vlans = $doc->findnodes("//x:vlan-information/x:vlan"); print "found ", scalar @vlans, " vlans\n"; foreach my $vlan (@vlans){ my @parameters = $vlan->findnodes("./x:vlan-name"); print "found ", scalar @parameters, " parameters\n"; }
Output:
found 0 vlans The root element's namespace is: urn:ietf:params:xml:ns:netconf:base:1 +.0 found 1 vlans found 1 parameters

In reply to Re: XML namespace question by tangent
in thread XML namespace question by perlific

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.