繁体   English   中英

InDesign使用PHP导出XML解析嵌套列表

[英]InDesign export XML parse nested lists with PHP

我有一个从InDesign导出的XML,我需要将其转换为HTML。 除了项目符号和列表,其他所有内容都很简单。 InDesign将XML导出为:

<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>

我在PHP5中将XML手动解析为HTML。 上一个片段应为:

<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>

当它是简单(一级)列表时,没有问题。 说到嵌套列表,我已经完成了。 可以使用任何<paragraph>停止该列表,并且应正确关闭所有以前创建的列表。

任何建议都非常欢迎。

那就是我要考虑的XSLT;)

在周末我创建了一个解决方案。 当然,可能会有更好的方法,但是我不熟悉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);
    }
}

非常感谢大家对我的问题的关注。 祝你今天愉快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM