ritvik_raj2012 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
please help me with a code for how to find the self enclosing tag like "<prompt/>" or a tag with empty data like "<prompt></prompt>" in a XML file. thanks in advance.
  • Comment on searching a empty XML tag or self enclosing tags

Replies are listed 'Best First'.
Re: searching a empty XML tag or self enclosing tags
by Anonymous Monk on Apr 19, 2012 at 14:11 UTC
      thanks a lot guys,
      but i want to actually list out all the empty elements in the excel.or print in the console which are empty or do not have any values or self enclosing tags also in between them.

        so print instead of deleting. In xsh, ls is essentially printing stuff.

Re: searching a empty XML tag or self enclosing tags
by choroba (Cardinal) on Apr 19, 2012 at 14:14 UTC
    I usually use XML::XSH2 for XML manipulation. To search for nodes, it uses XPath. In this case, you can use:
    ls //*[0=count(./* | ./@* | ./text() | ./comment() | ./processing-inst +ruction())]
    (You did not specify what to do with comments and processing instructions. You might need to change the XPath accordingly.)

      I wouldn't wish xsh on my worst enemies

        Can you elaborate? What is your favourite tool?

        Regarding the OP and my reply, the XPath expression can be used in any other tool supporting XPath.