我有一个XML文件和一个HTML文件,并希望将其合并到一个新文档中。
对我来说,这也是我不是PHP开发人员,也是第一次做这样的事情。
这样的结果应成为Word文档...
将生成此文件(来源: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>
实际上,这将是导出的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>></td>
<td>Rol</td>
<td>:</td>
<td>[rol]</td>
</tr>
<tr>
<td> </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是此基础)。
您正在编写模板系统。这里确实存在很多模板系统。对于XML / HTML,这是一种称为XSLT的特定语言,可以用来对其进行转换。
但是,您正在使用自己的占位符语法(而不是属性或标签)。对于XSLT,您需要执行以下操作:
<tr>
<td> </td>
<td>Opdrachtgever</td>
<td>:</td>
<td><t:text name="opdrachtgever"/></td>
</tr>
或者您使用HTML5样式:
<tr>
<td> </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文档导入数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句