首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT嵌套TOC

XSLT嵌套TOC
EN

Stack Overflow用户
提问于 2014-12-17 14:09:47
回答 1查看 145关注 0票数 0

它应该解析XML文件中的内容表,并输出带有有序列表的HTML。我尝试了如下所示,但它显示重复的条目(第一个标题两次)和一个空的li标记,我不知道如何删除它们。

我的XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<document>
    <section>
        <title>Section One</title>
    </section>
    <section>
        <title>Section Two</title>
        <section>
            <title>Section Two.One</title>
        </section>
        <section>
            <title>Section Two.Two</title>
            <section>
                <title>Section Two.Two.One</title>
            </section>
        </section>
    </section>
    <section>
        <title>Section Three</title>
    </section>
</document>


My XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="document">
        <html>

        <head>
        </head>

        <body>
            <ol>
                <xsl:apply-templates/>
            </ol>
        </body>

        </html>
    </xsl:template>

    <xsl:template match="section">
        <li>

            <xsl:value-of select="title" />
            <xsl:if test="section">
                <ol>
                    <li>
                        <xsl:apply-templates select="title" />
                        <xsl:if test="section">
                            <li>

                                <xsl:apply-templates select="section" />
                            </li>
                        </xsl:if>

                    </li>
                </ol>
            </xsl:if>
        </li>

    </xsl:template>

</xsl:stylesheet>

产出:

会不会是分组问题?我有顺序元素,我想在没有空li标记的情况下嵌套它。任何想法都会受到极大的赞赏。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-17 14:21:57

我相信你让这件事变得更复杂了。这样试一试:

代码语言:javascript
复制
<xsl:template match="/document">
    <html>
        <head>
        </head>
        <body>
            <ol>
                <xsl:apply-templates select="section" />
            </ol>
        </body>
    </html>
</xsl:template>

<xsl:template match="section">
    <li>
        <xsl:value-of select="title" />
        <xsl:if test="section">
            <ol>
                <xsl:apply-templates select="section" />
            </ol>
        </xsl:if>  
    </li>
</xsl:template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27527472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档