snahl
snahl

Reputation: 503

XSLT: stop list output on condition if-test (or something)

I've got an XBEL formatted XML-file. The out-put is a nested list. I wish to out-put only till the 2nd level (included) of the nodes named 'folder'. The match rule is set accordingly in the template. Of course the resulting HTML output contains an empty -tag. How can I do this properly, so that the template stops producing output before creating empty <ul>-tags (without <li>-tags)? I assume the solution is in specifying the test-condition properly.

The Template:

<xsl:template match="xbel/folder/folder/folder" mode="subfolderlist">
    <li>
        <span class="folderTitleLink"><xsl:value-of select="title" /></span>
    </li>
    <xsl:apply-templates mode="subfolderlist" />
</xsl:template>

The XSLT:

...<!--<xsl:if test="not(xbel/folder/folder/folder)">-->
<xsl:if test="(child::folder)">     <!--do it as long as there are subfolders, the last node gets never listed-->
    <div class="level-2">
        <ul>
            <xsl:apply-templates mode="subfolderlist" />
        </ul>
    </div>
</xsl:if>...

The XML structure:

<xbel>
<folder folded="yes">
    <title>bookmarks</title>
    <desc>my bookmarks</desc>

    <folder folded="no">
        <title>Level-1</title>
        <desc>bla1</desc>
        <bookmark href="http://www.xyz.com/">
            <title>BM1-Level1</title>
            <desc>Desc-BM1-Level1</desc>
        </bookmark>

        <folder folded="no">
            <title>Level-2</title>
            <desc>bla2</desc>
            <bookmark href="http://www.xyz.com/">
                <title>BM1-Level2</title>
                <desc>Desc-BM1-Level2</desc>
            </bookmark>

            <folder folded="no">
                <title>Level-3</title>
                <desc>bla3</desc>
                <bookmark href="http://www.xyz.com/">
                    <title>BM1-Level3</title>
                    <desc>Desc-BM1-Level3</desc>
                </bookmark>

                <folder folded="no"> 
                    <title>Level-4</title>
                    <desc>bla4</desc>
                    <bookmark href="http://www.xyz.com/">
                        <title>BM1-Level4</title>
                        <desc>Desc-BM1-Level4</desc>
                    </bookmark>
                </folder>
            </folder>
        </folder>
    </folder>
</folder></xbel>

here

Upvotes: 1

Views: 466

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

You need to have a template with a match pattern as the following:

<xsl:template match="folder[not(ancestor::folder[3])]" 
              mode="subfolderlist"> 

  <!-- Your processing here -->

</xsl:template>

Also, replace this:

<xsl:if test="(child::folder)">

with:

<xsl:if test="folder and not(ancestor::folder[2])">  

Upvotes: 1

Related Questions