如何在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>

包含内联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是此基础)。

W

您正在编写模板系统。这里确实存在很多模板系统。对于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属性的任何元素: //*[@data-template-content]

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Emacs中模仿Sublime Text的HTML dom完成?

来自分类Dev

如何在Emacs中模仿Sublime Text的HTML dom完成?

来自分类Dev

在iOS中,UIWebView如何在完成加载之前获取html DOM内容?

来自分类Dev

如何在html dom中编写

来自分类Dev

我如何在XML中传递html?

来自分类Dev

如何在 HTML 中编写 XML

来自分类Dev

如何在PHPStorm中禁用HTML标记完成

来自分类Dev

如何在 C# 中的 XML 到 JSON 转换期间忽略 HTML 内容的#cdata 部分

来自分类Dev

如何在Javascript中将混合的HTML字符串/ DOM元素转换为DOM元素?

来自分类Dev

如何在<?php?>中编写html代码

来自分类Dev

如何在PHP中解析HTML?

来自分类Dev

如何在html文件中调用php

来自分类Dev

如何在GWT中访问HTML对象内的DOM

来自分类Dev

如何在Capybara中获取DOM元素的HTML?

来自分类Dev

如何在HTML结构中添加DOM模型?

来自分类Dev

如何在HTML的影子DOM中添加子级?

来自分类Dev

如何在PHP中将HTML <TAGS>转换为<tags>?

来自分类Dev

如何在Python中将.php.html转换为csv

来自分类Dev

如何在php中将html文件转换为word文件

来自分类Dev

如何从DOM对象转换为html?

来自分类Dev

如何从 html 转换为 php

来自分类Dev

转换HTML DOM

来自分类Dev

转换HTML DOM

来自分类Dev

PHP简单HTML DOM:如何找到JavaScript中存在的Urls

来自分类Dev

如何将dom html推入php中的数组?

来自分类Dev

PHP简单HTML DOM:如何找到JavaScript中存在的Urls

来自分类Dev

如何在Angular JS中的HTML中显示XML文本

来自分类Dev

如何合并PHP和HTML主题代码?

来自分类Dev

如何合并php和html代码?