in reply to XML tag closing
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML tag closing (naive way)
by maha (Novice) on Jan 06, 2012 at 10:34 UTC | |
|
Re^2: XML tag closing (naive way)
by maha (Novice) on Jan 06, 2012 at 09:10 UTC | |
by cavac (Prior) on Jan 07, 2012 at 13:01 UTC |