Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Amir Khan 1199 posts 2567 karma points
    Jul 22, 2009 @ 21:33
    Amir Khan
    0

    Improper nesting and linking of list

    I have this code which is generating nested lists for navigation under a specific parent node. For some reason the <a href... is wrapping the lists oddly and itsn't actually closing after each <li> any ideas what I'm doing wrong?

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <!-- Don't change this, but add a 'contentPicker' element to -->
    <!-- your macro with an alias named 'source' -->
    <xsl:variable name="source" select="1090"/>

    <xsl:template match="/">
        <xsl:call-template name="drawNodes">
            <xsl:with-param name="parent" select="umbraco.library:GetXmlNodeById($source)" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="drawNodes">
        <xsl:param name="parent" />
        <ul>
        <xsl:for-each select="$parent/node">
            <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName" />
                <xsl:if test="count(./node) &gt; 0">
                    <!-- this node has children, let's list them also -->
            <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:call-template name="drawNodes">
                        <xsl:with-param name="parent" select="." />
                    </xsl:call-template>
            </a>
                </xsl:if>
        </a>
            </li>
        </xsl:for-each>
        </ul>
    </xsl:template>

    </xsl:stylesheet>

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Jul 22, 2009 @ 21:39
    Jan Skovgaard
    100

    Hi Amir

    This part of your code is wrong

        <xsl:for-each select="$parent/node">
            <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName" />
                <xsl:if test="count(./node) &gt; 0">
                    <!-- this node has children, let's list them also -->
            <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:call-template name="drawNodes">
                        <xsl:with-param name="parent" select="." />
                    </xsl:call-template>
            </a>
                </xsl:if>
        </a>
            </li>
        </xsl:for-each>

     

    the last closing </a> tag should be placed before your if test like below

        <xsl:for-each select="$parent/node">
            <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName" />
    </a>
                <xsl:if test="count(./node) &gt; 0">
                    <!-- this node has children, let's list them also -->
            <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:call-template name="drawNodes">
                        <xsl:with-param name="parent" select="." />
                    </xsl:call-template>
            </a>
                </xsl:if>
            </li>
        </xsl:for-each>

     

    Hope this helps

    /Jan

  • Amir Khan 1199 posts 2567 karma points
    Jul 22, 2009 @ 21:44
    Amir Khan
    0

    Perfect, fixed. Thank you Jan!

  • Amir Khan 1199 posts 2567 karma points
    Jul 22, 2009 @ 21:45
    Amir Khan
    0

    Also, I found that the the second <a href is redunant. The code below, with your fix, links the whole list and nested lists appropriately.

     

    <xsl:for-each select="$parent/node">
            <li>
                <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName" />
            </a>
                <xsl:if test="count(./node) &gt; 0">
                    <!-- this node has children, let's list them also -->

                    <xsl:call-template name="drawNodes">
                        <xsl:with-param name="parent" select="." />
                    </xsl:call-template>
           
                </xsl:if>

            </li>
        </xsl:for-each>

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies