【问题标题】:How can this DOM translation or merging XML/HTML be done in PHP?如何在 PHP 中完成这种 DOM 翻译或合并 XML/HTML?
【发布时间】:2016-10-26 17:09:15
【问题描述】:

我有一个 XML 文件和一个 HTML 文件,并且想将它们合并到一个新文档中。

对我来说这也是第一次做这样的事情,因为我不是 PHP 开发人员。

这样的结果应该变成word文档了……

XML 文件

会生成这个文件(来源:drupal)

<professie>Manager</professie>
<gebdate>1960</gebdate>
    <project>
        <rol>Projectmanager</rol>
        <opdrachtgever>Apple</opdrachtgever>
        <result>Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum </result>
        <time>2012-2013</time>
    </project>

    <project>
        <rol>Teamleader</rol>
        <opdrachtgever>Google</opdrachtgever>
        <result>Lorum at google ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum </result>
        <time>2011-2014</time>
    </project>

包含内联 css 的 HTML 模板文件

实际上,这将是一个导出的 word 文档,格式为 html 过滤的文件

<table style="width: 100%;">
    <tbody>
        <tr>
            <td>Profession</td>
            <td>[professie]</td>
        </tr>
        <tr>
            <td>Date of birth</td>
            <td>[gebdate]</td>
        </tr>
    </tbody>
</table>

<project>
<table style="width: 100%;">
    <tbody>
        <tr>
            <td>&gt;</td>
            <td>Rol</td>
            <td>:</td>
            <td>[rol]</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>Opdrachtgever</td>
            <td>:</td>
            <td>[opdrachtgever]</td>
        </tr>
        <tr>
            <td></td>
            <td>Resultaat</td>
            <td>:</td>
            <td>[result]</td>
        </tr>
        <tr>
            <td></td>
            <td>Datum</td>
            <td>:</td>
            <td>[time]</td>
        </tr>
    </tbody>
</table>

所以我构建了一个函数来执行此操作。目前我构建这样的伪代码:

<?php

function generatemydocument ($path_to_content_file,$path_to_template_file,$path_to_output_file){

    if (!file_exists($path_to_xml_file)){
        return;
    }
    if (!file_exists($path_to_template_file)){
        return;
    }
    if (file_exists($path_to_output_file)){
        // make log message output file already exist
        return;
    }

    // read the file into a string
    $templatefile = readfileascompletestring ($path_to_template_file)

    // Search and replace all single dom elemelens with no children

    // Get all elements from dom document with no childs and put it in a array
    // Reallly nu clu how to do this yet ...
    $array_with_strings_to_replace = array("all elements from $path_to_content_file with NO children","all values for this element")

    // init
    $dbData = array(); 
    foreach ($array_with_strings_to_replace as $key => $value)
        $sanitizedValue = strip_tags(ucfirst(strtolower($value)));
        $templatefile = str_replace('{$'.$key.'}', $sanitizedValue, $templatefile);
        $dbData[$key] = mysql_real_escape_string($sanitizedValue);
        // add a line of code into a log file
    }   

    // Search and replace all nodes with the use of a dom translation

    $content = new DOMDocument();
    $content->loadXML($path_to_content_file);

    $template = new DOMDocument();
    $template ->loadHTML($templatefile);

    // Create a new document
    $newdoc = new DOMDocument;
    $newdoc->formatOutput = true;

    // Set the template in the newdoc
    $newdoc = $template

    // Import the node, and all its children, to the document
    $node = $newdoc->importNode($node, true);

    // I think  I have to do some replacements here but I'm a little bit lost in here

    // And then append it to the "<root>" node
    $newdoc->documentElement->appendChild($node);

    $newdoc->saveHTML($path_to_output_file  );

}
?>

但是我对这个php的dom库了解的越多,我就有一些疑问。

1) 现在,我对没有子节点的节点执行字符串替换操作。我觉得这可以用 DOMNode DOMDocument::importNode 来完成,并且 importnode 只是导入带有或不带有子节点的节点。对吗?

2) 我不明白如何处理内容和模板中节点上的合并。我也应该替换并搜索它吗?

3) 我不认为这是这个世界上的新事物。是否有一个库函数可以做到这一点?

该函数不应包含来自 dom 文档本身的任何信息,我可以使用我们需要合并这个完整事物的信息来更改函数的输入变量,例如包含所有元素名称的列表。

这可以更容易地完成吗?

最后这应该是drupal7中的一个模块,用户在系统中选择一个节点(这将给出content.xml),这个节点将被下载到一个word文档中(template.html是这个的基础)。

【问题讨论】:

    标签: php html xml drupal ms-word


    【解决方案1】:

    您正在编写一个模板系统。这里确实存在很多模板系统。对于 XML/HTML,这里有一种称为 XSLT 的特定语言,可用于对其进行转换。

    但是您使用自己的占位符语法(不是属性或标签)。对于 XSLT,您需要执行以下操作:

    <tr>
      <td>&nbsp;</td>
      <td>Opdrachtgever</td>
      <td>:</td>
      <td><t:text name="opdrachtgever"/></td>
    </tr>
    

    或者你使用 HTML5 样式:

    <tr>
      <td>&nbsp;</td>
      <td>Opdrachtgever</td>
      <td>:</td>
      <td data-template-content="opdrachtgever"></td>
    </tr>
    

    元素和属性可以与 Xpath 1.0 匹配。

    • 特定模板命名空间中的任何元素://t:*
    • 具有数据属性的任何元素://*[@data-template-content]

    通过 Xpath 匹配的模板逻辑,您可以使用 DOM 和 DOMXpath 在 PHP 中实现它。 DOMDocument::importNode() 确实从第二个文档中导入并克隆了一个节点。如果您使用 DOM 方法,它们将处理编码和转义问题。

    使用 XSLT 将为您提供必要的模板逻辑,例如循环和条件。它也可以从其他 XML 文档中导入数据。

    【讨论】:

    • 谢谢,我研究了 XSLT 并做了一些测试,我肯定会用 XSLT 语法替换我的示例“HTML 模板”。
    猜你喜欢
    • 2011-05-10
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多