I looked in several question and documents and parts of code around but do not have a good idea what I'm doing wrong.
I need to combine 8 different XML's into one XML which eventually will be 1 html file. The filenames of the XML are dynamically generated. In the example I use only 2 xml files.
XML1 in variabele : $file_xml_cv
<?xml version="1.0" encoding="UTF-8"?>
<node>
<cv>
<cvid>584</cvid>
<titel>Dit is een test cv</titel>
<naam>DHR H.V.H. Dagobert Duck</naam>
</cv>
</node>
XML2 in variabele $file_xml_werkgevers
<?xml version="1.0" encoding="UTF-8"?>
<node>
<werkgevers>
<naam>Company 1</naam>
<Functie>Projectmanager en Informatie analist</Functie>
<periode>1967-01-01 00:00:00</periode>
<einddatum>1967-01-01 00:00:00</einddatum>
</werkgevers>
<werkgevers>
<naam>Company 2</naam>
<Functie>Systeemontwerper</Functie>
<periode/>
<einddatum/>
</werkgevers>
</node>
Which should result in XML3
<?xml version="1.0" encoding="UTF-8"?>
<node>
<cv>
<cvid>584</cvid>
<titel>Dit is een test cv</titel>
<naam>DHR H.V.H. Dagobert Duck</naam>
</cv>
<werkgevers>
<naam>Company 1</naam>
<Functie>Projectmanager en Informatie analist</Functie>
<periode>1967-01-01 00:00:00</periode>
<einddatum>1967-01-01 00:00:00</einddatum>
</werkgevers>
<werkgevers>
<naam>Company 2</naam>
<Functie>Systeemontwerper</Functie>
<periode/>
<einddatum/>
</werkgevers>
</node>
In order to do translation to html with XSLT i have:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:m="http://www.example.com/"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2><xsl:value-of select="node/cv/naam"/></h2>
<xsl:if test="node/werkgevers">
<table border="1">
<tr bgcolor="#9acd32">
<th>Werkgevers</th>
<th></th>
<th></th>
<th></th>
</tr>
<xsl:for-each select="node/werkgevers">
<tr>
<td>>></td>
<td><xsl:value-of select="periode"/></td>
<td><xsl:value-of select="naam"/></td>
<td><xsl:value-of select="functie"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Currently I combine the first and second XML with string manipulations which is not a very good solution I think. I tried several php code but could not solve it.
Currently I merge now 1 xml with 1 xslt with the following code which is working good.
// Load the XML source
$xml_cv = new DOMDocument;
$xml_cv->load($file_xml_total);
// Load the XLS
$xsl_cv = new DOMDocument;
$xsl_cv->load($file_xsl_cv);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl_cv); // attach the xsl rules
$proc->transformToURI($xml_cv, $file_html_cv);
So the question is:
1) How to combine more XML files into 1 XML file so I can use the same PHP code as mentioned?
or
2) Another solution could be to make use of XMLT 2.0 document function to combine different XML trough 1 xslt but I could not find any solution how to deal with this as the names of the files are dynamic. One of the answers of this is: XSLT: Merging two log files with different structure and time-representation
But in here there are fixed names as mentioned in:
<!-- The source-documents. -->
<xsl:variable name="doc1" select="doc('log1.xml')"/>
<xsl:variable name="doc2" select="doc('log2.xml')"/>
How to solve this second approach?
Development based on new Input
I'm a little bit lost... Based on the new input I looked into it and I had troubles with getting the same results. After a few hours it looked that the processing
$proc->transformToXML
is not working so I went on to make it ever more simpel. So now after many hours i have the following very simple approach:
The code:
$file_xsl_merge = 'C:\www\arlande.nl\sites\default\files\node_export\merge.xsl';
$xsl = new DOMDocument('1.0', 'UTF-8');
$xsl->load($file_xsl_merge);
writetolog ("Dump of xslt: ". $xsl->savexml());
$initXML = '<?xml version="1.0" encoding="UTF-8"?><node><dummy>33</dummy></node>';
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->LoadXML ($initXML);
writetolog ("Dump of XML: ". $xml->savexml());
// TRANSFORM XML
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$newXML = $proc->transformToXML($xml,$file_xml_total);
writetolog ("A dump of xml after processing is: ". $xml->savexml());
writetolog ("String newXML is : ". $newXML);
writetolog( "All XML should be combine now in file : " . $file_xml_total);
The merge.xls:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- The source-documents. -->
<xsl:variable name="doc1" select="document('C:\www\arlande.nl\sites\default\files\node_export\cv.xml')"/>
<xsl:variable name="doc2" select="document('C:\www\arlande.nl\sites\default\files\node_export\werkgevers.xml')"/>
<xsl:variable name="doc3" select="document('C:\www\arlande.nl\sites\default\files\node_export\opleiding.xml')"/>
<xsl:variable name="doc4" select="document('C:\www\arlande.nl\sites\default\files\node_export\opdracht_b.xml')"/>
<xsl:variable name="doc5" select="document('C:\www\arlande.nl\sites\default\files\node_export\opdracht_s.xml')"/>
<xsl:template match="node">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="$doc1/node/*"/>
<xsl:copy-of select="$doc2/node/*"/>
<xsl:copy-of select="$doc3/node/*"/>
<xsl:copy-of select="$doc4/node/*"/>
<xsl:copy-of select="$doc5/node/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I created a log file from this code and that is displaying:
16:22:00 Start run
16:22:00 s:1186:"Dump of xslt: <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- The source-documents. -->
<xsl:variable name="doc1" select="document('C:\www\arlande.nl\sites\default\files\node_export\cv.xml')"/>
<xsl:variable name="doc2" select="document('C:\www\arlande.nl\sites\default\files\node_export\werkgevers.xml')"/>
<xsl:variable name="doc3" select="document('C:\www\arlande.nl\sites\default\files\node_export\opleiding.xml')"/>
<xsl:variable name="doc4" select="document('C:\www\arlande.nl\sites\default\files\node_export\opdracht_b.xml')"/>
<xsl:variable name="doc5" select="document('C:\www\arlande.nl\sites\default\files\node_export\opdracht_s.xml')"/>
<xsl:template match="node">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="$doc1/node/*"/>
<xsl:copy-of select="$doc2/node/*"/>
<xsl:copy-of select="$doc3/node/*"/>
<xsl:copy-of select="$doc4/node/*"/>
<xsl:copy-of select="$doc5/node/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
";
16:22:00 s:83:"Dump of XML: <?xml version="1.0" encoding="UTF-8"?>
<node><dummy>33</dummy></node>
";
16:22:00 s:105:"A dump of xml after processing is: <?xml version="1.0" encoding="UTF-8"?>
<node><dummy>33</dummy></node>
";
16:22:00 s:19:"String newXML is : ";
16:22:00 s:108:"All XML should be combine now in file : C:/www/arlande.nl/sites/default/files/node_export/node_584_total.xml";
So again the processing of the document function in xslt is not working...
- I tried several combinaties in the search match /node.
- All seperate XML's files are there and all starting with ...
Any suggestion how to get this working?