I have an XML data in the following form :
<table>
<col>
<name>Addresses</name>
</col>
<col>
<name>Addresses/Address1</name>
</col>
<col>
<name>Addresses/Address2</name>
</col>
<col>
<name>Addresses/Address1/Flat Number</name>
</col>
<col>
<name>Employee Name</name>
</col>
<col>
<name>Phone Number</name>
</col>
<col>
<name>Profession</name>
</col>
<col>
<name>Employee Name/First Name</name>
</col>
<col>
<name>Employee Name/Last Name</name>
</col>
<col>
<name>Employee Name/First_Name</name>
</col>
<col>
<name>Accounts/Account Name/First/Saving</name>
</col>
<col>
<name>Accounts/Account_Name/Second</name>
</col>
</table>
Now I want to create a treeview like structure in HTML using XSLT on this XML. The structure of the Treeview would be similar to following structure :
- Addresses
- Address1
- Address2
- Employee Name
- First Name
- Last Name
- Accounts
- Account Name
Please note that substring before first occurrence of "/" is the First Level Node of treeview and the substring after the first occurrence of "/" is the Second Level Node of the matching First Level Node, and this is only a 2 level treeview.
Also, the values displayed are unique ones. Even if the values are repeating in the XML, we need to pick only unique one value. One more condition to consider is that values with "" are considered same as without "" as given in example : "First Name" and "First_Name", so we need to display the value after replace "-" with " ".
<xsl:variable name="currentNode" select="//table/col" />
<xsl:key name="uniqueCategoryKey" match="record" use="name"/>
<xsl:key name="uniqueSubCategoryKey" match="record" use="substring-before(substring-after(concat(name,'/'),'/'),'/')"/>
<xsl:template match="/">
<xsl:call-template name="treeTemplate" />
</xsl:template>
<xsl:template name="treeTemplate">
<div id ="newtreeview">
<ul>
<xsl:for-each select="$currentNode[generate-id() = generate-id(key('uniqueCategoryKey', name))]">
<xsl:variable name="category" select="name"/>
<xsl:if test="string-length($category) > 0 and not(contains($category,'/'))">
<li>
<a href="#">
<xsl:value-of select="$category"/>
</a>
<xsl:if test="//record[contains(name, concat($category,'/'))]">
<ul>
<xsl:for-each select="$currentNode[generate-id() = generate-id(key('uniqueSubCategoryKey', substring-before(substring-after(concat(name,'/'),'/'),'/')))]">
<li>
<a href="#">
<xsl:variable name="subcat">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="string(substring-before(substring-after(concat(name,'/'),'/'),'/'))" />
<xsl:with-param name="replace" select="'_'" />
<xsl:with-param name="by" select="' '" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$subcat"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</xsl:if>
</xsl:for-each>
</ul>
</div>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text"
select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The problem is that, this code is giving me an output like
- Addresses
-
-
-
- Address1
- Address2
-
- Employee Name
-
-
-
- First Name
- First Name
- Last Name
-
- Accounts
- Account Name
- Account Name
Please help me solve this issue.
Thanks in Advance.