I am new to XSLT and have been struggling with the following problem. I appreciate if anyone could help me how to go around this.
This is my XML file, but the element names could differ each time. The XML is created on the run. Basically I don't know all the elements in the XML file. There could be more or less elements, but basically has the following structure:
<University>
<language>en</language>
<name>Medi University</name>
<location>Rome</location>
<country>Italy</country>
<member>
<teacher>
<name>John Sting</name>
<joined>
<time>
<start/>
<end/>
</time>
<valid>true</valid>
</joined>
<name>Paul Ironman</name>
<joined>
<time>
<start/>
<end/>
</time>
<valid>true</valid>
</joined>
</teacher>
<teacherAssistant>
<name>Luna Tutti</name>
<joined>
<time>
<start>1.9.2015</start>
<end></end>
</time>
<valid>true</valid>
</joined>
</teacherAssistant>
</member>
<telephone>7538476398754</telephone>
<email>medi@medi.com</email>
</University>
I have this XSLT file that tries to transform that. As I said the XML file is created on run time and I don't know the XML content.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="name()"/>
<xsl:value-of select="text()"/>
<xsl:if test="*">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The above code prints the CSV file like this:
elemenNameelementValue
elemenName2elementValue2
elemenName3elementValue3
and so on.
What I want is something like bellow:
University
language, name, location, country,telephone, email
english, Medi, Rome,Italy,7538476398754,medi@medi.com
Teacter
name, joined, time, start,end,valid,
John Sting, , , , ,true
Paul Ironman, , , , , true
Teacher Assistant
name, joined, time, start,end,valid,
Luna Tutti, , ,1.9.2015, , true
I want related elements to appear on one line as in above.
Thanks
You could use the identity template (as in Using XSLT to copy all nodes in XML, with support for special cases) and work from there.
As an example, with this code
The output is:
(note that I had to put a final in your XML
Try this truly generic stylesheet
As mentioned by michael.hor257k, without some constraints, new table starts for each node having child nodes.
Try this for replacement