【问题标题】:XSLT passing variables inside nested for-each loopsXSLT 在嵌套的 for-each 循环中传递变量
【发布时间】:2013-12-17 16:41:03
【问题描述】:

我在这方面还很陌生,我不确定是否可以在这里实现我想要实现的目标。我有 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>

【问题讨论】:

  • 您是否有示例输入 XML 和所需的输出 XML?
  • 我想要的输出是根据每个部分和段落的顺序编号,例如 1.1.1 格式

标签: xslt tableofcontents


【解决方案1】:

如果没有关于输入和输出的更多细节,这应该可以工作。值得注意的是,变量名在根目录,以允许所有模板访问它。但由于每章似乎有一个 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>

【讨论】:

  • 哇,太棒了!这有很大帮助,我的 xml 看起来和我发布的有点不同,但由于它的巨大尺寸,我不得不将它切到骨头上。我将不得不做一些小的改变,但这绝对是我想要的方向,非常感谢你,伙计。竖起大拇指和所有好东西:)
  • 如果这个答案对你有帮助,请不要忘记标记它(左边的勾号)。
猜你喜欢
  • 1970-01-01
  • 2021-12-27
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多