Thanks everyone for your insightful (and entertaining?) comments - yes, this is the OP, but being new I may have contravened best practice for changing my username (my apologies to the gods, and other fellow monks). The suggested solutions were all very helpful in understanding the nuances of namespaces (no namespace vs. default namespace and so on) and the multiple ways to come at this sort of problem. I have a further question now with regard to attributes. Whats the best method to pull out the value of a given attribute (like junos:style="terse" in the vlan-information element), especially when the attribute is in a different namespace as the containing element. Using the same XML snippet as in the original post:

my $dom = XML::LibXML->load_xml( string => $jnx->{'server_response'}, # parser options ... ); my $xpc = XML::LibXML::XPathContext->new(); $xpc->registerNs('nc', 'urn:ietf:params:xml:ns:netconf:base:1.0'); $xpc->registerNs('j', 'http://xml.juniper.net/junos/12.3R9/junos'); my @nodes = $xpc->findnodes( "/nc:rpc-reply/nc:vlan-information", $dom +); foreach my $node (@nodes){ print "\nAttempt 1\n"; my $attr_style = $node->findvalue('/@style'); $attr_style ? print "Got Attr _style_: $attr_style\n" : print "Cou +ldn't find Attr _style_\n"; print "\nAttempt 2\n"; my $test1 = $node->hasAttributes(); print " Yes I have attribute(s)\n" if $test1; print "\nAttempt 3\n"; my @attrs = $node->attributes('/style'); foreach my $attr (@attrs){ print "here's an attribute: $attr->to_literal\n"; } print "\nAttempt 4\n"; my $attrnode = $node->getAttributeNodeNS( 'http://xml.juniper.net/ +junos/12.3R9/junos', 'style'); print "here's an attribute: $attrnode\n"; }

My attempts above (the last two at least) yield a result but its not ideal... I want to say for attribute "style" in namespace "junos", what is the value? Thanks!


In reply to Re: XML namespace question by perlific
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.