I am attempting to convert a comma delimited list into an XML file with a hierarchical structure. To do this, I am using XSLT alone, preferably with one transform. There is a previous example that is similar, but it does not go into the depth of creating subelements, which I see is a common problem during this kind of transformation that to my knowledge has been left without a clear answer.
Similar Example: XSLT 2.0 to convert CSV to XML format
CSV Example:
ClaimRef,HandlerRef,ClaimType,Date,Area,SettleDate,ClaimStatus,ClaimantName
1,1/1,Liability,08-12-2013,US,23-05-2014,Closed,Mark
2,1/2,Liability,08-10-2013,UK,23-02-2014,Closed,John
Desired XML Output Format (Where this is different because it contains subelements):
<Claims>
<Claim>
<ClaimRef></ClaimRef>
<HandlerRef></HandlerRef>
<ClaimType></ClaimType>
<Date></Date>
<Area></Area>
<SettleDate></SettleDate>
<ImportantDevision>
<ClaimStatus></ClaimStatus>
<ClaimantName></ClaimantName>
</ImportantDivision>
</Claim>
</Claims>
Working XSLT Version 2.0 Without Subelements:
<xsl:param name="inputCsv"/>
<xsl:template match="/" name="csv2xml">
<Claims>
<xsl:variable name="csv" select="unparsed-text($csv-uri, $csv-encoding)"/>
<!--Get Header-->
<xsl:variable name="header-tokens" as="xs:string*">
<xsl:analyze-string select="$csv" regex="\r\n?|\n">
<xsl:non-matching-substring>
<xsl:if test="position()=1">
<xsl:copy-of select="tokenize(.,',')"/>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:analyze-string select="$csv" regex="\r\n?|\n">
<xsl:non-matching-substring>
<xsl:if test="not(position()=1)">
<Claim>
<xsl:for-each select="tokenize(.,',')">
<xsl:variable name="pos" select="position()"/>
<xsl:element name="{$header-tokens[$pos]}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Claim>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</Claims>
</xsl:template>
I would then have a dummy XML file with in order trick the XSL to transform my CSV file. Perhaps a better question would be how to distinguish divisions from one another using only XSLT before Tag Names, attributes, ids, etc. are created?