in reply to Splitting XML file on Processing Instructions
You would need to add file I/O error handling, and perhaps handle cases of missing tags. I assumed an input file named data. I assumed that the value inside <h1>...</h1> was used in the file name, because there is no reference to 'test' in the file, and perhaps you meant 'text' instead. But despite the caveats, this does something like you wanted:
#! /usr/bin/perl -w use strict; my $text; if (open INPUT, '<data') { local $/; $text = <INPUT>; close INPUT; } while ($text =~ /<\?split \?>(.*?)(?=<\?split \?>)/sg) { my $fragment = $1; my ($h1) = $fragment =~ /<h1>(.*?)<\/h1>/is; my ($from, $to) = $fragment =~ /<no>(.*?)<\/no>/isg; if (open OUTPUT, ">${h1}-nr${from}to${to}.xml") { print OUTPUT $fragment; close OUTPUT; } } exit 0;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Splitting XML file on Processing Instructions
by Anonymous Monk on Jun 24, 2004 at 08:19 UTC | |
by pbeckingham (Parson) on Jun 24, 2004 at 13:11 UTC |