0

如果这是已经在这里彻底讨论过的事情,我深表歉意。我花了很多时间寻找答案,但似乎无法找到一个明确的答案。此外,在 PHP 方面,我几乎是个业余爱好者。

我有一个 XML 文档,我正在尝试使用 PHP 将其呈现为 HTML。XML 实际上“理论上”用于单一来源,这意味着希望将其用作生成 PDF 和 HTML 输出的来源。因此,XML 实际上是由充当手册部分的元素组成的。见下文:

<topic>
   <title>Some topic.</title>
   <caution>
      <para>Cautionary note 1.</para>
   </caution>
   <para>Paragraph 1</para>
   <caution>
      <para>Cautionary note 2.</para>
   </caution>
   <para>Paragraph 2</para>
   <figure>
      <graphic src="../some_path"/>
   </figure>
</topic>

对于 PDF,我计划编写 XSLT 转换。对于 HTML,我有一个脚本可以成功地将 XML 的所有元素转换为 HTML。问题是,它似乎将 XML 转换为已解析的数组。上面的示例使用(使用 HTML 等效项)呈现:

  • 顶部的所有图形元素。
  • 中间的所有 para 元素(不包括 attention 元素中的 para 元素)。
  • 底部的所有警告元素。

因此,不保留文档顺序。由于此来源用于整理手册,因此文档顺序很重要。

我的问题:是否可以使用 PHP 将 XML 解析为 HTML 并保留文档顺序?同样,我几乎是 PHP 业余爱好者,所以如果我的问题显得迟钝,我深表歉意。欢迎任何建议或指示。

编辑添加一些 PHP

PHP 脚本非常冗长,因此我将尝试包含尽可能多的相关位。

就注意事项而言,脚本最初定义了一个函数:

//Display the cautions from the xml
        function display_cautions($cautions)
        {
            foreach( $cautions as $caution )
            {
                foreach( $caution->para as $cautionpara )
                {
                    printf("<p class='Text'>");
                    foreach( $cautionpara->{'attention-icon'} as $attentionicon )
                    {
                        printf("<img src='../../images/%s' width='25px' height='25px'>", htmlspecialchars($attentionicon['srcfile']));
                    }
                    $cautionpara = str_ireplace('<emph>', '<b>', $cautionpara);
                    $cautionpara = str_ireplace('</emph>', '</b>', $cautionpara);
                    printf("%s</p>", htmlspecialchars($cautionpara));
                }
            }
        }

然后它构建文档:

if (isset($_GET['ID']))  
        {
            $introsections = $doc->{'intro-section'};
            //printf("got here 1");
            foreach( $introsections as $introsection )
            {
                //printf("got here 2");
                $sectno = $introsection['sect-no'];
                if ($sectno == $_GET['ID'])
                {
                    $smtitle = $introsection->title->nodeValue;
                    $introgenerals = $introsection->{'intro-general'};
                    foreach( $introgenerals as $introgeneral )
                    {
                        //printf("got here 3");
                        $smtitle = $introgeneral->title;
                        $introid = $introgeneral['id'];
                        //Display the title
                        printf("<h2>%s</h2>", htmlspecialchars($smtitle));
                            //Match found
                            $topics = $introgeneral->topic;
                            foreach( $topics as $topic )
                            {
                                //Display the title
                                printf("<p class='Text12Bold'>%s</p>", htmlspecialchars($topic->title));
                                $topicparas = $topic->para;
                                //Get all the paragraphs
                                foreach( $topicparas as $topicpara )
                                {
                                    //MH added: code to include xrefs here
                                    //check for xrefs in this para
                                    $xrefs = $topicpara->xref;
                                    if (simple_xml_count($xrefs) > 0) {
                                        //printf("<p class='Text'>%s</p>\n", htmlspecialchars($topicpara));
                                        display_referral_links($xrefs, $topicpara);
                                    } else {
                                        $topicpara = str_ireplace('<emph>', '<b>', $topicpara);
                                        $topicpara = str_ireplace('</emph>', '</b>', $topicpara);
                                        ?>
                                        <p class='Text'><?php printf($topicpara) ?></p>

                                        <?php
                                    }
                                }

然后它调用警告函数:

//Get all the cautions with images where applicable
display_cautions($topic->caution);

在文档构造和函数调用之间,它构造了表格、子主题和图形。警告、注释和列表也有类似的功能。

4

0 回答 0