I am trying to get around xslt. A few questions here are about stackoverflow help ( XSLT templates and recursion as well as XSLT for each loop, variable based filter ), but I'm still a bit puzzled. I guess I'm βthinking of a template as a functionβ ( https://stackoverflow.com/questions/506348/how-do-i-know-my-xsl-is-efficient-and-beautiful )
Anyway ... my details
<Entities> <Entity ID="8" SortValue="0" Name="test" ParentID="0" /> <Entity ID="14" SortValue="2" Name="test2" ParentID="8" /> <Entity ID="16" SortValue="1" Name="test3" ParentID="8" /> <Entity ID="17" SortValue="3" Name="test4" ParentID="14" /> <Entity ID="18" SortValue="3" Name="test5" ParentID="0" /> </Entities>
What I would like as an output is basically a "treeview"
<ul> <li id="entity8"> test <ul> <li id="entity16"> test3 </li> <li id="entity14"> test2 <ul> <li id="entity17"> test4 </li> </ul> </li> </ul> </li> <li id="entity18"> test5 </li> </ul>
XSLT I'm still mistaken in that he definitely "thinks of templates as a function" and also throws a StackOverflowException (:-)) when executed
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="html" indent="yes"/> <xsl:template match="Entities"> <ul> <li> <xsl:value-of select="local-name()"/> <xsl:apply-templates/> </li> </ul> </xsl:template> <xsl:template match="//Entities/Entity[@ParentID=0]"> <xsl:call-template name="recursive"> <xsl:with-param name="parentEntityID" select="0"></xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template name="recursive"> <xsl:param name="parentEntityID"></xsl:param> <xsl:variable name="counter" select="//Entities/Entity[@ParentID=$parentEntityID]"></xsl:variable> <xsl:if test="count($counter) > 0"> <xsl:if test="$parentEntityID > 0"> </xsl:if> <li> <xsl:variable name="entityID" select="@ID"></xsl:variable> <xsl:variable name="sortValue" select="@SortValue"></xsl:variable> <xsl:variable name="name" select="@Name"></xsl:variable> <xsl:variable name="parentID" select="@ParentID"></xsl:variable> <a href=?ID={$entityID}&ParentEntityID={$parentID}"> <xsl:value-of select="$name"/> </a> <xsl:call-template name="recursive"> <xsl:with-param name="parentEntityID" select="$entityID"></xsl:with-param> </xsl:call-template> </li> </xsl:if> </xsl:template> </xsl:stylesheet>
I know how to do this with code, no problem. This time, however, I am looking for a solution in xslt, and any help would be very appreciated for this.
source share