DigitalKitty has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

I'm currently in the process of teaching myself how to use XPath and Perl to parse XML documents (most notably, the chatterbox feed). After reading the documentation, I felt compelled to inquire about removing the attribute name in the output (please see below):

<?xml version="1.0" encoding="ISO-8859-1"?> <CHATTER> <INFO site="" sitename="PerlMonks" ticker_id="158 +34" gentimeGMT="2007-01-05 23:29:59" xmlstyle="old" xmlmaker="XML::Fling 1.001" count="3" lastid= +"419432"> Rendered by the Chatterbox XML Ticker</INFO> <message user_id="22609" author="tye" time="20070105182057"> is "this proxy" that you mentioned relatively new?</message> <message user_id="170442" author="jdporter" time="20070105182124"> is that a problem?</message> <message user_id="170442" author="jdporter" time="20070105182217"> oh, sorry. yes, looks like [merlyn|he] is having a problem.</message> </CHATTER>

By using the short program provided by the module's author and what I had already gleaned from various online tutorials, I was able to display the value of each 'author' attribute:

#!/usr/bin/perl use warnings; use strict; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new(filename => 'pm.xml'); my $nodeset = $xp->find('//@author'); # find all authors foreach my $node ($nodeset->get_nodelist) { print XML::XPath::XMLParser::as_string($node), "\n\n"; }


C:\Perl\bin>perl author="tye" author="jdporter" author="jdporter" C:\Perl\bin>

Is there a method I'm unaware of that will display only the value(s) and omit the atttribute 'name='(?)


Replies are listed 'Best First'.
Re: Perl and XPath.
by duckyd (Hermit) on Jan 06, 2007 at 01:09 UTC
    use getNodeValue:
    foreach my $node ($nodeset->get_nodelist) { print $node->getNodeValue, "\n\n"; }
    See perldoc XML::XPath::Node::Attribute
Re: Perl and XPath.
by jettero (Monsignor) on Jan 06, 2007 at 01:02 UTC

    I use the following in a project (real path obscured):

    for my $pattern ($path->findnodes("thingy/something", $top)) { my $file = $path->findvalue('@file', $pattern)->value; my $num = $path->findvalue('@number', $pattern)->value; print "number is $num and file is $file\n"; }

    If there's a better way, I'd like to know it. I find the XML::XPath to be a little awkward to use, but I think I'm missing something.


Re: Perl and XPath.
by Jenda (Abbot) on Jan 08, 2007 at 00:27 UTC
Re: Perl and XPath.
by murugu (Curate) on Jan 07, 2007 at 08:55 UTC
    getNodeValue method will give you the authors name.
    #!/usr/bin/perl use warnings; use strict; my $a = do {local $/; <DATA>}; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new(xml => $a); my $nodeset = $xp->find('//@author'); # find all authors foreach my $node ($nodeset->get_nodelist) { print $node->getNodeValue."\n\n"; }

    Murugesan Kandasamy
    use perl for(;;);

      Is it possible to use getNodeValue if you want to get values from multiple attributes at a single level in the tree. For example <id = "2" name = "Jim" Date = "Today" > So in my $node variable I have all of the values. How can I use getNodeValue to access them individually