如何在PHP中完成此DOM翻译或合并XML/HTML



我有一个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>

模板HTML文件,包括Inline CSS

实际这将是以格式html过滤的文件导出的Word文档

<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 :: enciptnode来完成这项操作,并且exixtnode只是带有或不带有儿童节点的节点。这是正确的吗?

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

3)我没有这个世界在这个世界上是新的。是否有可以执行此操作的库功能?

该函数不应包含来自DOM文档本身的任何信息,我可以将功能的输入变量与我们需要合并此完整内容的信息,例如元素的所有名称。

可以更轻松地完成操作吗?

最后,这应该是drupal7中的一个模块,在该模块中,用户在系统中选择一个节点(这将给出content.xml),并且该节点将在Word Document中下载(Template.html是此的基础))。

您正在编写模板系统。这里确实存在许多模板系统。对于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文档中导入数据。

最新更新