Hello,

I've been playing with, and trying to learn, XML::LibXML. I've been trying to get the values from specific nodes based on one of its attributes. I can get and print "all" the node values, and I can get and print just the specific attribute values, but I can't seem to figure out how to print the node values of nodes with specific attributes.

Here is the xml I'm using. I'd like to grab the values from the <Data> nodes, but only the ones with the "name" attribute that equals "NAME_2".

<xml> <Document> <Name>Places To Visit</Name> <Folder> <Place> <Name>Location 1</Name> <ExtendedName> <Data name="NAME_0">United States</Data> <Data name="NAME_1">Utah</Data> <Data name="NAME_2">Salt Lake City</Data> </ExtendedName> </Place> <Place> <Name>Location 2</Name> <ExtendedName> <Data name="NAME_0">United States</Data> <Data name="NAME_1">Rhode Island</Data> <Data name="NAME_2">Providence</Data> </ExtendedName> </Place> <Place> <Name>Location 3</Name> <ExtendedName> <Data name="NAME_0">United States</Data> <Data name="NAME_1">Wisconsin</Data> <Data name="NAME_2">Green Bay</Data> </ExtendedName> </Place> <Place> <Name>Location 4</Name> <ExtendedName> <Data name="NAME_0">United States</Data> <Data name="NAME_1">Wyoming</Data> <Data name="NAME_2">Casper</Data> </ExtendedName> </Place> </Folder> </Document> </xml>

Here is the small bit of code that I'm using to pull out the contents of the attributes, but I don't know how to get the node values. I hope that makes sense.

Thank you for your help!

#!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $xml = XML::LibXML->load_xml(location => 'locations.xml'); foreach my $node2 ($xml->findnodes('//Place')) { my $attr; foreach( $node2->findnodes('./ExtendedName/Data/@name') ) { $attr = $_->textContent(); if($attr eq "NAME_2" ) { print $_->textContent() . "\n"; } } }

In reply to XML Node Values Based On Attributes by gpjahn

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.