Reputation: 19
I have a problem processing an xml file with xslt.
<?xml version="1.0" encoding="UTF-8"?>
<center name="center" code="11111" version="1.0">
<teachers>
<teacher>
<name>My name</name>
<surname>My surname</surname>
<groupsubject>2CFSA#2CFS0653</groupsubject>
<groupsubject>2CFSA#2CFS0655</groupsubject>
<groupsubject>1CFSA#1CFS0647</groupsubject>
<groupsubject>1CFSA#1CFS0648</groupsubject>
</teacher>
<teachers>
</center>
I want to create a CSV file with all this data, for each teacher: name,surname,group1,group2,group3,...,groupN This is my XSLT code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="teachers">
<xsl:for-each select="teacher">
<xsl:value-of select="name"/>,<xsl:value-of select="surname"/>,
<xsl:for-each select="groupsubject">
<xsl:value-of select="."/>,
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The problem is that the foreach sentence creates a new file for each groupsubject, instead of add the content to the same line. I know this is correct, but I can't know how can extract all the teacher data in only one line (csv line). Anyone can help me? Regards
Upvotes: 1
Views: 398
Reputation: 545
The carriage returns in your XSLT, after the commas, are being literally interpreted. Use the <xsl:text>,</xsl:text>
to prevent this.
ie.
<xsl:template match="teachers">
<xsl:for-each select="teacher">
<xsl:value-of select="name"/><xsl:text>,</xsl:text><xsl:value-of select="surname"/><xsl:text>,</xsl:text>
<xsl:for-each select="groupsubject">
<xsl:value-of select="."/><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Upvotes: 2