]
I am new to XSLT, and I need help with passing a parameter into the XSLT based on a link.
Here is what I am trying to do:
I need to build a dynamic navigation tree.
On a client-side transformation, using MSXML, I would like to use one XML file with one XSL stylesheet to get my HTML, but I need the output to depend on (a parameter) the link that is selected.
Here is a Sample XML:
<categories>
<category name="Jewelry" link="jewelry">
<level name="Gold" link="#">
<sublevel name="Rings" link="#"/>
<sublevel name="Watches" link="#"/>
<sublevel name="More..." link="#"/>
</level>
<level name="Silver" link="#">
<sublevel name="Rings" link="#"/>
<sublevel name="Watches" link="#"/>
<sublevel name="More..." link="#"/>
</level>
</category>
<category name="Computers & Electronics" link="#">
<level name="Computers" link="#">
<sublevel name="Desktops" link="#"/>
<sublevel name="Laptops" link="#"/>
</level>
<level name="Electronics" link="#">
<sublevel name="Stereos" link="#"/>
<sublevel name="Televisions" link="#"/>
</level>
</category>
<category name="Art & Antiques" link="#">
<level name="Art" link="#">
<sublevel name="Paintings" link="#"/>
<sublevel name="Posters" link="#"/>
<sublevel name="More..." link="#"/>
</level>
<level name="Antiques" link="#"></level>
</category>
</categories>
And what I want to happen is to have a list of all the CATEGORY names in HTML like so:
<p><a href="#">Jewelery</a></p>
<p><a href="#">Computers & Electronics</a></p>
<p><a href="#">Art & Antiques</a></p>
But, when you click on say Jewelry link, I only want the information about the LEVELs inside the Jewelry category to show up in the HTML, and nothing else, like so:
<p><a href="#">Jewelery</a>
[list]
<li><a href="#">Gold</a></li>
<li><a href="#">Silver</a></li>
</ul>
</p>
<p><a href="#">Computers & Electronics</a></p>
<p><a href="#">Art & Antiques</a></p>
And, if you click further, lets say on Gold, I would want only Gold to expand like so:
<p><a href="#">Jewelery</a>
[list]
<li><a href="#">Gold</a>
[list]
<li><a href="#">Rings</a></li>
<li><a href="#">Watches</a></li>
<li><a href="#">moreâ¦</a></li>
</ul>
</li>
<li><a href="#">Silver</a></li>
</ul>
</p>
<p><a href="#">Computers & Electronics</a></p>
<p><a href="#">Art & Antiques</a></p>
I am not sure how to create my XSL stylesheet so that I can use only one XML file that has the information about all the CATEGORIES, and transform it so that only the information that I want is output into the HTML.
Here is a sample stylesheet with Parameters set up:
<xsl:param name="category" select="'Computers'" />
<xsl:param name="level" select="'Electronics'" />
<xsl:template match="categories/category">
<xsl:variable name="showLevel" select="starts-with(@name, $category)" />
<xsl:for-each select=".">
<p>
<a href="{@link}">
<xsl:value-of select="@name" />
</a>
<xsl:choose>
<xsl:when test="$showLevel">
[list]
<xsl:apply-templates select="level" />
</ul>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</p>
</xsl:for-each>
</xsl:template>
<xsl:template match="level">
<xsl:variable name="showSublevel" select="starts-with(@name, $level)" />
<xsl:for-each select=".">
<li><a href="{@link}"><xsl:value-of select="@name"/></a>
<xsl:choose>
<xsl:when test="$showSublevel">
<xsl:choose>
<xsl:when test="sublevel">
[list]
<xsl:apply-templates select="sublevel" />
</ul>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</li>
</xsl:for-each>
</xsl:template>
<xsl:template match="sublevel">
<xsl:for-each select=".">
<li><a href="{@link}"><xsl:value-of select="@name"/></a></li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I am not sure where to go from here. How can I pass the parameter into my XSL based on the link?
I probably have to use JavaScript to pass the parameter, but not sure how.
Or is there another way?
Any help would be greatly appreciated.
Thank you,
LISKA