XSL,元素数量的变量

时间:2016-12-20 13:11:05

标签: xml variables xslt count

我必须在我的xsl文档中创建一个变量,它将计算我的xml文件中的元素,然后在最终输出中显示这个数字。在这里,我认为我正确地做了变量声明,很可能没有。如何正确创建这个变量然后"打印它"在HTML部分 感谢

<xsl:template match="/">
    <html>
        <head>
            <title>Albumy Burzum</title>

            <style type="text/css">

            h2 { font-size: 40px; font-family: arial; color: #000000;  }

            </style>


        </head>
        <body>
            <h2>Albumy Burzum</h2>
             <table border="5">
                <tr>
                    <th>name</th>
                    <th>year</th>
                    <th>genre</th>
                    <th>label</th>
                    <th>songs_number</th>
                    <th>link</th>

                </tr>
            <xsl:apply-templates />
        </table>

        <h3>Number of elements:</h3>


        </body>
    </html>
</xsl:template>





<xsl:attribute-set name="reference">
    <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
</xsl:attribute-set>

<xsl:template match="link">

    <xsl:element name="a" use-attribute-sets="reference"><xsl:value-of select="link"/></xsl:element>
    <br/>
</xsl:template>

<xsl:template match="album">
  <tr> 
    <td><xsl:value-of select="name"/></td>
    <td><xsl:value-of select="year"/></td>
    <td><xsl:value-of select="genre"/></td>
    <td><xsl:value-of select="label"/></td>
    <td><xsl:value-of select="songs_number"/></td>
    <td><xsl:value-of select="link"/></td>

  </tr>
</xsl:template>

<xsl:variable name="countElements">
    <xsl:value-of select="count(album)"/>

</xsl:variable>

1 个答案:

答案 0 :(得分:0)

好吧,你甚至没有向我们展示XML输入结构,并解释了你想要计算哪些元素,但是如果用<h3>Number of elements:</h3>替换<h3>Number of elements: <xsl:value-of select="count(//album)"/></h3>那么你应该得到任何计数的输出文档中的album元素,完全不使用变量。