Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Perl and XPath. by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-05-29 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found