my $xmlish = <<'END'; <whatever><book-part type='closed'> Chapter 1 </book-part> <book-part type='unclosed'> Chapter 2 <book-part type='final'> Chapter 3 </whatever> END my $prev = '/'; $xmlish =~ s{(?=<\s*(/?)\s*book-part\b)}{ ( ! $prev && ! $1 ? '</book-part>' : '', $prev= $1, )[0] }ge; print $xmlish; __END__ <whatever><book-part type='closed'> Chapter 1 </book-part> <book-part type='unclosed'> Chapter 2 </book-part><book-part type='final'> Chapter 3 </whatever>
Assumes no <book-part bits inside attribute values, comments, nor CDATA blocks, of course. Not using an XML parser since missing closing tags means you don't really have XML that a real XML parser would handle. Also note how it doesn't insert a final closing tag (you didn't provide enough information for me to know how to do that for your case).
- tye
In reply to Re: XML tag closing (naive way)
by tye
in thread XML tag closing
by maha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |