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="http://perlmonks.org/" 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"; }
Output:
C:\Perl\bin>perl test.pl
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='(?)
Thanks,
~Katie
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.