XSLT:条件if-test(或其他)时停止列表输出

时间:2011-08-24 23:26:13

标签: xml templates xslt conditional-statements

我有一个XBEL格式的XML文件。输出是一个嵌套列表。我希望只输出名为'folder'的节点的第二级(包含)。匹配规则在模板中相应设置。 当然,生成的HTML输出包含一个空-tag。如何正确执行此操作,以便模板在创建空<ul> - 标记(不包含<li> - 标记)之前停止生成输出? 我假设解决方案是正确指定测试条件。

模板:

<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>

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>...

XML结构:

<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>

这里

1 个答案:

答案 0 :(得分:1)

您需要具有匹配模式的模板,如下所示

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

  <!-- Your processing here -->

</xsl:template>

另外,请替换

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

<强>与

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