[Home] [By Thread] [By Date] [Recent Entries]
On 2/13/07, Simon Shutter <simon@xxxxxxxxxxx> wrote:
If I have a tabular data set that defines parent-child relationships, is it possible to transform this into a hierarchical tree format using XSLT?
<xsl:output indent="yes" encoding="US-ASCII"/> <xsl:param name="pathToCSV" select="'file:///c:/temp/test.csv'"/> <xsl:template name="main">
<xsl:choose>
<xsl:when test="unparsed-text-available($pathToCSV)">
<xsl:variable name="csv" select="unparsed-text($pathToCSV)"/>
<xsl:variable name="lines" select="tokenize($csv, '
')" as="xs:string+"/>
<xsl:variable name="elemNames" select="tokenize($lines[1], ',')"
as="xs:string+"/>
<root>
<xsl:for-each select="$lines[position() > 1]">
<row>
<xsl:variable name="lineItems"
select="tokenize(normalize-space(.), ',')" as="xs:string+"/>
<xsl:for-each select="$elemNames">
<xsl:variable name="pos" select="position()"/>
<xsl:element name="{normalize-space(.)}">
<xsl:value-of select="$lineItems[$pos]"/>
</xsl:element>
</xsl:for-each>
</row>
</xsl:for-each>
</root>
</xsl:when>
<xsl:otherwise>
<xsl:text>Cannot locate : </xsl:text><xsl:value-of select="$pathToCSV"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
|

Cart



