您可以使用任何语言,允许您直接操纵xml。我建议用DOM而不是SAX找到一些东西。如果你使用SAX,你必须自己遍历xml--这是我的经验。 DOM允许您以更多的OOP方式对xml进行操作。
一些立即跳入脑海的东西将是xml“文档”的包装xml。
因此,像:
<documents>
<document>
<!-- Your xml here -->
</document>
<document>
<!-- Your xml here -->
</document>
<document>
<!-- Your xml here -->
</document>
</documents>
伪代码将是: 创建文档根目录。 添加一个名为documents的元素,将其用作根。 迭代每个xml文件。 为每个文件创建一个名为document的新元素。将该元素添加到父级。从文件加载xml。将该节点导入外部文档。将导入的节点附加到文档元素子集合中。
编辑 如这里承诺的是更新的代码进行了测试,我知道作品:
<?php
// Replace the strings below with the actual filenames, add or decrease as fit
$filenames = array(0 => "test.xml", 1 => "test2.xml", 2 => "test3.xml");
$docList = new DOMDocument();
$root = $docList->createElement('documents');
$docList->appendChild($root);
foreach($filenames as $filename) {
$doc = new DOMDocument();
$doc->load($filename);
$xmlString = $doc->saveXML($doc->documentElement);
$xpath = new DOMXPath($doc);
$query = "//product"; // this is the name of the ROOT element
$nodelist = $xpath->evaluate($query, $doc->documentElement);
if($nodelist->length > 0) {
$node = $docList->importNode($nodelist->item(0), true);
$xmldownload = $docList->createElement('document');
$xmldownload->setAttribute("filename", $filename);
$xmldownload->appendChild($node);
$root->appendChild($xmldownload);
}
}
echo $docList->saveXML();
?>
来源
2011-05-06 20:06:46
SRM
好问题,+1。查看我的答案,获得一个简洁而又简单的纯XSLT解决方案。 :) – 2011-05-07 02:56:16