Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
<image href = "images/cron1.png"><<alt>></alt></image>

More than a single example would be better. Note that <image> is "... an ancient and poorly supported precursor to the <img> element. It should not be used." whereas <img> is an empty element, meaning it can't have content. <img hrefsrc="images/cron1.png" alt="" /> would be a valid HTML example.

But assuming your example is accurate, i.e. it is not actually HTML (and ignoring the likely erroneous <<alt>>), you can use Mojo::DOM in XML mode to parse your data. I've made a guess that by "empty" you mean that it does not contain other tags or non-whitespace text, but please clarify this as well.

use warnings; use strict; use Mojo::DOM; my $html = <<'END_HTML'; <image href = "images/cron1.png"></image> <image href = "images/cron2.png"> </image> <image href = "images/cron3.png">abc</image> <image href = "images/cron4.png"><alt></alt></image> <image href = "images/cron5.png"><alt><br/></alt></image> <image href = "images/cron6.png"><i><alt> </alt></i></image> <image href = "images/cron7.png"><alt>xyz</alt></image> <image href = "images/cron8.png"><alt><b>xyz</b>ijk</alt></image> <image href = "images/cron9.png">abc <alt><!-- foo --></alt> def</imag +e> END_HTML my $dom = Mojo::DOM->new->xml(1)->parse($html); $dom->find('image alt')->grep(sub { # find all <alt> tags inside <imag +e> not $_->child_nodes->grep(sub { # check whether they have content $_->type eq 'text' || $_->type eq 'cdata' ? $_->content=~/\S/ : $_->type ne 'comment' })->size; })->map('remove'); print $dom; __END__ <image href="images/cron1.png" /> <image href="images/cron2.png"> </image> <image href="images/cron3.png">abc</image> <image href="images/cron4.png" /> <image href="images/cron5.png"><alt><br /></alt></image> <image href="images/cron6.png"><i /></image> <image href="images/cron7.png"><alt>xyz</alt></image> <image href="images/cron8.png"><alt><b>xyz</b>ijk</alt></image> <image href="images/cron9.png">abc def</image>

Minor edits; changed selector from 'image > alt' to 'image alt' to find all descendants. Also: Comments are now not considered as content.


In reply to Re: Replace empty alt tag on <img> tag by haukex
in thread Replace empty alt tag on <img> tag by vlearner

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 about the Monastery: (5)
As of 2024-03-28 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found