在周末,我创造了一个解决方案。当然,可能有更好的方法,但我不熟悉XSLT,所以我在纯PHP中尝试了它。以下是我的解决方案:
preg_match_all('@<Cell>(.*?)</Cell>@', $xml, $cells);
$cells = $cells[1];
function appendLists(&$v) {
global $ol, $ol2, $ul, $ul2, $olc, $ol2c, $ulc, $ul2c;
if (count($ol) > 0 || count($ol2) > 0 || count($ul) > 0 || count($ul2) > 0) {
while (preg_match('@~!ol1-(\d+)-here!~@u', $v, $m)) {
$v = str_replace($m[0], '<ol><li>' . join('</li><li>', $ol[$m[1]]) . '</li></ol>', $v);
}
while (preg_match('@~!ul1-(\d+)-here!~@u', $v, $m)) {
$v = str_replace($m[0], '<ul><li>' . join('</li><li>', $ul[$m[1]]) . '</li></ul>', $v);
}
while (preg_match('@~!ol2-(\d+)-here!~@u', $v, $m)) {
$v = str_replace($m[0], '<ol><li>' . join('</li><li>', $ol2[$m[1]]) . '</li></ol>', $v);
}
while (preg_match('@~!ul2-(\d+)-here!~@u', $v, $m)) {
$v = str_replace($m[0], '<ul><li>' . join('</li><li>', $ul2[$m[1]]) . '</li></ul>', $v);
}
$ol = array(); $ol2 = array(); $ul = array(); $ul2 = array();
$olc = -1; $ol2c = -1; $ulc = -1; $ul2c = -1;
}
}
foreach ($cells as $cell) {
$output = '';
if ($cell != '') {
// split to elements
preg_match_all('@<(.*?)>(.*?)</\1>@u', $cell, $elements);
$ol = array(); $ol2 = array(); $ul = array(); $ul2 = array();
$olc = -1; $ol2c = -1; $ulc = -1; $ul2c = -1;
for ($i = 0; $i < count($elements[0]); $i++) {
$this_element = $elements[1][$i];
$this_value = $elements[2][$i];
$next_element = (isset($elements[1][$i+1]) ? $elements[1][$i+1] : false);
switch ($this_element) {
case 'paragraph':
$output .= '<p>' . $this_value . '</p>';
if ($next_element === 'ord_list1') { $olc++; $ol[$olc] = array(); $output .= "~!ol1-$olc-here!~"; }
elseif ($next_element === 'list1') { $ulc++; $ul[$ulc] = array(); $output .= "~!ul1-$ulc-here!~"; }
elseif ($next_element === 'ord_list2') { $ol2c++; $ol2[$ol2c] = array(); $output .= "~!ol2-$ol2c-here!~"; }
elseif ($next_element === 'list2') { $ul2c++; $ul2[$ul2c] = array(); $output .= "~!ul2-$ul2c-here!~"; }
break;
case 'ord_list1':
if ($output == '') { $olc++; $ol[$olc] = array(); $output .= "~!ol1-$olc-here!~"; }
if ($next_element === 'ord_list2') { $ol2c++; $ol2[$ol2c] = array(); $this_value .= "~!ol2-$ol2c-here!~"; }
elseif ($next_element === 'list2') { $ul2c++; $ul2[$ul2c] = array(); $this_value .= "~!ul2-$ul2c-here!~"; }
$ol[$olc][] = $this_value;
break;
case 'ord_list2':
if ($output == '') $output .= '~!ol2-here!~';
$ol2[$ol2c][] = $this_value;
break;
case 'list1':
if ($output == '') { $ulc++; $ul[$ulc] = array(); $output .= "~!ul1-$ulc-here!~"; }
if ($next_element === 'ord_list2') { $ol2c++; $ol2[$ol2c] = array(); $this_value .= "~!ol2-$ol2c-here!~"; }
elseif ($next_element === 'list2') { $ul2c++; $ul2[$ul2c] = array(); $this_value .= "~!ul2-$ul2c-here!~"; }
$ul[$ulc][] = $this_value;
break;
case 'list2':
if ($output == '') { $ul2c++; $ul2[$ul2c] = array(); $output .= "~!ul2-$ul2c-here!~"; }
$ul2[$ul2c][] = $this_value;
break;
}
}
appendLists($output);
}
}
非常感谢大家对我问题的关注。
祝您有个美好的一天。