I am trying to take a CSV file as input and transform it into a XML. I'm new to XSLT and I've found a way to convert a CSV into XML (using an example from Andrew Welch) like so:
Input CSV file:
car manufacturer,model,color,price,inventory
subaru,outback,blue,23195,54
subaru,forester,silver,20495,23
And my output XML would be:
<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row>
<column name="car manufacturer">subaru</column>
<column name="model">outback</column>
<column name="color">blue</column>
<column name="price">23195</column>
<column name="inventory">54</column>
</row>
<row>
<column name="car manufacturer">subaru</column>
<column name="model">forester</column>
<column name="color">silver</column>
<column name="price">20495</column>
<column name="inventory">23</column>
</row>
</rows>
My desired output is actually something similar to:
<stock>
<model>
<car>subaru outback</car>
<color>blue</color>
<price>23195</price>
<inventory>54</inventory>
</model>
<model>
<car>subaru forester</car>
<color>silver</color>
<price>20495</price>
<inventory>23</inventory>
</model>
</stock>
What I read is that it would best be done using a two phase transformation. The CSV to XML is done using XSLT 2.0, so I thought the two phase transformation would be done using that as well without using the node-set function.
So the first phase would be to take the original CSV file as input, and then output the intermediate XML shown above. Then take that intermediate XML, and pass it into another transformation to get the desired output.
Anyone can help on how the two phase transformation can be done? I'm having trouble passing the output of phase one as an input of phase 2?
I have something like this so far:
<xsl:import href="csv2xml.xsl"/>
<xsl:output method="xml" indent="yes" />
<xsl:variable name="intermediate">
<xsl:apply-templates select="/" mode="csv2xml"/>
</xsl:variable>
<xsl:template match="rows" name="main">
**[This is what I'm having trouble with]**
</xsl:template>