http://qs1969.pair.com?node_id=11140666


in reply to Replace empty alt tag on <img> tag

Please see It is uncool to update a node in a way that renders replies confusing or meaningless and mark your updates as such. It's the part starting with:

Here is my tried implementation. what changes do I need to make to get the desired output?

Your example is not complete (SSCCE) because it does not compile, does not show any input data, or most importantly which XML/HTML module you are using. My best guess at the moment based on the method names is XML::DOM. If that is the case, then the minimum change needed to your code to get it to work* is to change if($kids->item($k)->toString() =~ /<alt>/i) to if ( $kids->item($k)->getNodeType==ELEMENT_NODE && $kids->item($k)->getTagName eq 'alt' ).

* Update: "work" in this case meaning, to actually have an effect on the single example you provided. This doesn't address the question of only removing empty tags, but your approach of iterating over the list of children would work there as well, inspecting each one to see whether you consider it "content" or not, similar to what I do in my Mojo example.

Update 2: You edited the root node again. As I said above, don't do that, post a new question.