I'm going to transform the structure of this xml file to another one (with new element names) by taking some data from there, namely:
- Value of all elements;
- Value of all and elements from elements;
I'm just started to play with XSLT and have weak knowledge, so don't judge strictly me. My transform.xsl template is (without printing xml element names):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="/">
<xsl:apply-templates select="/RESPONSE/MULTIPLE/SINGLE/KEY" />
</xsl:template>
<!-- transformation to another xml file -->
<xsl:template match="/MULTIPLE">
<course>
<xsl:for-each select="SINGLE">
<topic>
<!-- Updated -->
<chapter><xsl:value-of select="KEY[@name='name']/VALUE" /></chapter>
<xsl:for-each select="KEY[@name='modules']/MULTIPLE/SINGLE">
<title><xsl:value-of select="KEY[@name='name']/VALUE" /></title>
<content><xsl:value-of select="KEY[@name='description']/VALUE" /></content>
</xsl:for-each>
<!-- /Updated -->
</topic>
</xsl:for-each>
</course>
</xsl:template>
Expected stucture is [updated]:
<?xml version="1.0" encoding="UTF-8"?>
<course>
<topic>
<chapter>Chapter Name 1</chapter>
<title>Title Name 1</title>
<content>Content 1</content>
</topic>
<!-- Updated -->
<topic>
<chapter>Chapter Name 1</chapter> <!-- print for each <title> and <content> -->
<title>Title Name 2</title>
<content>Content 2</content>
</topic>
<topic>
<chapter>Chapter Name n</chapter>
<title>Title Name n</title>
<content>Content n</content>
</topic>
<!-- Updated -->
...
</course>
and php procedure:
$xml = new DOMDocument;
$xml->load("http://dl.dropbox.com/u/72519118/response.xml");
$xsl = new DOMDocument;
$xsl->load("transform.xsl");
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
Any help would be appreciated.