I have an exported XML from InDesign and I need to convert it into HTML. Everything is simple except bullets and lists. InDesign exports XML like:
<paragraph>First paragraph</paragraph>
<list_level_1>List 1 - Item 1</list_level_1>
<list_level_1>List 1 - Item 2</list_level_1>
<list_level_1>List 1 - Item 3</list_level_1>
<list_level_2>List 1 - Item 3.1</list_level_2>
<list_level_2>List 1 - Item 3.2</list_level_2>
<list_level_1>List 1 - Item 4</list_level_1>
<paragraph>Second paragraph</paragraph>
<list_level_1>List 2 - Item 1</list_level_1>
<list_level_1>List 2 - Item 2</list_level_1>
<paragraph>Third paragraph</paragraph>
I parse XML to HTML manually in PHP5. The previous snippet should then be:
<p>First paragraph</p>
<ol>
<li>List 1 - Item 1</li>
<li>List 1 - Item 2</li>
<li>List 1 - Item 3<ol>
<li>List 1 - Item 3.1</li>
<li>List 1 - Item 3.2</li>
</ol></li>
<li>List 1 - Item 4</li>
</ol>
<p>Second paragraph</p>
<ol>
<li>List 2 - Item 1</li>
<li>List 2 - Item 2</li>
</ol>
<p>Third paragraph</p>
When it is simple (one-level) list, no problem. When it comes to nested lists, I am finished. The list can be stopped with any <paragraph> and all previously created lists should be properly closed.
Any advice is highly welcome.
during the weekend I created a solution. Of course there might be better ways, but I am not familiar with XSLT so I tried it in pure PHP. Here is my solution:
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);
}
}
Many thanks to all of you for the attention to my problem. Have a nice day.