I am trying to build a script that gets XMLs of different structures and create one unique XML containing all the data. A key point is that using an XSLT I rename/transform the nodes/elements because as I said the source XMLs have different structure. So the newly produced XML has a generic structure for me to use properly.
My question is how can I modify this to produce me a result of multiple XMLs?
The XMLs will be either relative or absolute path.
<?php
// create an XSLT processor and load the stylesheet as a DOM
$xproc = new XsltProcessor();
$xslt = new DomDocument;
$xslt->load('stylesheet.xslt'); // this contains the code from above
$xproc->i开发者_如何学PythonmportStylesheet($xslt);
// DOM or the source XML
$xml = '';
$dom = new DomDocument;
$dom->loadXML($xml);
?>
Hmm. Well, there's simply too many ways to skin this cat, but the two obvious ones are ;
- Import the XML files through the XSLT layer. This requires XSLT skills.
- Import the XML files through the PHP layer. This requires PHP skills.
First solution means you find a list of all the files you want to process, then create an XML file list from it like so which you pass in to the XSLT processor ;
<list>
<file src="..." />
<file src="..." />
<file src="..." />
</list>
Now you loop through (or match) these files, and use something like ;
<xsl:template match="file">
<xsl:apply-templates select="document(@src)/*" />
</xsl:template>
The other solution is to find all your files, and bind them together in a larger XML ;
$xml = "<all_files>" ;
foreach ( $files as $file )
$xml .= file_get_contents ( $file ) ;
$xml .= "</all_files>" ;
And have your XSLT apply templates as normal. This obviously requires some cleanup (like removing XML declarations and inline commands if there are any, possibly also DTDs and some entity stuff, but it's not too hard)
Both solutions require tweaks. What's your skills? What do you prefer to do? Do you need it to be memory saving? Fast? Concise? Flexible?
精彩评论