XSLT在嵌套for-each循环内传递变量

时间:2013-12-02 03:36:01

标签: xslt tableofcontents

我是相当新的,我不确定是否有可能做我想在这里实现的目标。我有3个for-each循环,为每个章节,段和段存储一个数字。我想从之前的for-each循环中获取存储的数字并将其显示在嵌套循环中,但我无法使其工作。

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

    <xsl:template match="/">
            <html>
                <head>
                    <title>Table of Contents, Chapter 3</title>
                </head>

                <body>
                    <xsl:for-each select="chapter">
                        <tr>
                            <xsl:variable name="capnr">
                                <xsl:number value="3" format="1. "/>
                            </xsl:variable>
                            <xsl:value-of select="$capnr"/>
                            <xsl:value-of select="@title"/>
                        </tr>
                        <br />

                        <xsl:for-each select="section">
                            <tr>
                                <xsl:variable name="secnr">
                                    <xsl:number format="1. "/>
                                </xsl:variable>
                                <xsl:value-of select="$capnr"/>
                                <xsl:value-of select="$secnr"/>
                                <xsl:value-of select="@title"/>
                            </tr>
                            <br />

                            <xsl:for-each select="paragraph">
                                <tr>
                                    <xsl:variable name="parnr">
                                        <xsl:number format="1 "/>
                                    </xsl:variable>
                                    <xsl:value-of select="$capnr"/>
                                    <xsl:value-of select="$secnr"/>
                                    <xsl:value-of select="$parnr"/>
                                    <xsl:value-of select="@title"/>
                                </tr>
                            </xsl:for-each>
                            <br />

                        </xsl:for-each>

                    </xsl:for-each>

                </body>

            </html>

        </xsl:template>


    </xsl:stylesheet>

XML:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ToC-3.xsl"?>
<chapter title="Chapter 3: Expressions">

<section title="Variables">
<paragraph title="Simple variables"></paragraph>
<paragraph title="Text variables"></paragraph>
<paragraph title="Remote identifiers"></paragraph>
</section>

<section title="The logical operators">
<paragraph title="Precedence of Boolean operators"></paragraph>
</section>

<section title="Designational expressions">
</section>

</chapter>

1 个答案:

答案 0 :(得分:0)

如果没有关于输入和输出的更多细节,这应该可行。值得注意的是,变量名称位于根目录上,以允许所有模板访问它。但由于每章似乎有一个XSl,这应该不是问题。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="capnr">
           <xsl:number value="3" format="1."/>
        </xsl:variable>    
    <xsl:template match="/">
        <html>
                <head>
                    <title>Table of Contents, Chapter 3</title>
                </head>

                <body>
                    <xsl:apply-templates/>
                </body>
        </html>
    </xsl:template>

    <xsl:template match="chapter">
                             <h1>        
            <xsl:value-of select="$capnr"/>
            <xsl:value-of select="@title"/>
        </h1>
        <ol><xsl:apply-templates/></ol>
    </xsl:template>

    <xsl:template match="section">
        <li>
            <xsl:value-of select="$capnr"/>
            <xsl:value-of select="position()"/>
            <xsl:text> - </xsl:text>
            <xsl:value-of select="@title"/>
            <ol><xsl:apply-templates/></ol>
        </li>
    </xsl:template>

    <xsl:template match="paragraph">
        <li>
            <xsl:value-of select="$capnr"/>
            <xsl:value-of select="count(../preceding-sibling::*) + 1"/>
            <xsl:text>.</xsl:text>
            <xsl:value-of select="position()"/>
            <xsl:text> - </xsl:text>
            <xsl:value-of select="@title"/>
            <ol><xsl:apply-templates/></ol>
        </li>
    </xsl:template>
</xsl:stylesheet>

当应用于给定输入XMl时,上面给出:

<html>
<head>
<title>Table of Contents, Chapter 3</title>
</head>
<body>
<h1>3.Chapter 3: Expressions</h1>
<ol><li>3.1 - Variables<ol><li>3.1.1 - Simple variables<ol></ol>
</li>
<li>3.1.2 - Text variables<ol></ol>
</li>
<li>3.1.3 - Remote identifiers<ol></ol>
</li>
</ol>
</li>
<li>3.2 - The logical operators<ol><li>3.2.1 - Precedence of Boolean operators<ol></ol>
</li>
</ol>
</li>
<li>3.3 - Designational expressions<ol></ol>
</li>
</ol>
</body>
</html>