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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|