我在分配计数器变量并递增它然后在XSLT中检查某个值时遇到了一些困难.这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" <xsl:variable name="empty_string"/>
<xsl:variable name="counter" select="0"/>
<xsl:template match="/Collection">
<xsl:for-each select="Content">
<xsl:sort select="Html/root/Event/start_date" order="ascending"/>
<xsl:variable name="isFutureEvent">
<xsl:value-of select="syscom:isFutureDate(Html/root/Event/start_date)" />
</xsl:variable>
<xsl:if test="Html/root/Event != $empty_string">
<xsl:if test="$isFutureEvent='true'">
<!-- Increment Counter -->
<xsl:value-of select="$counter + 1"/>
<!-- Test if Counter < 4 -->
<xsl:if test="$counter < 3">
<div class="media">
<!-- Do stuff here -->
</div>
</xsl:if> <!-- End if for counter -->
</xsl:if>
</xsl:if>
<!--</xsl:when>-->
<!--</xsl:choose>-->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
但它似乎没有增加我的计数器,并且当计数器击中时没有退出3.对此有任何帮助吗?
MiM*_*iMo 12
XSL中的"变量"实际上是常量 - 你无法改变它们的价值.这个:
<xsl:value-of select="$counter + 1"/>
Run Code Online (Sandbox Code Playgroud)
只会输出值 $counter+1
要做循环,你必须使用递归 - 例如:
<xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="loop">
<xsl:param name="i"/>
<xsl:param name="limit"/>
<xsl:if test="$i <= $limit">
<div>
<xsl:value-of select="$i"/>
</div>
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:call-template name="loop">
<xsl:with-param name="i" select="0"/>
<xsl:with-param name="limit" select="10"/>
</xsl:call-template>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
尽管最好避免循环 - 在大多数情况下,可以编写XSL来避免循环,但是我不太了解你想要实现什么来为你提供完整的解决方案.
小智 8
我有同样的问题.我需要在循环中增加值.所以最简单的方法是包括Saxon并使用该值.
如果你使用Saxon 6.5.5
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
...
xmlns:saxon="http://icl.com/saxon"
extension-element-prefixes="saxon"
version="3.0">
Run Code Online (Sandbox Code Playgroud)
如果你使用Saxon 9.4.0.4
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
...
xmlns:saxon="http://saxon.sf.net/"
extension-element-prefixes="saxon"
version="3.0">
Run Code Online (Sandbox Code Playgroud)
之后你可以简单地使用saxon变量:
<xsl:variable name="counter" select="0" saxon:assignable="yes"/> <!-- declare value -->
<saxon:assign name="counter" select="$counter+1"/> <!-- increment value, you can do it in loop for example-->
<xsl:value-of select="$counter"></xsl:value-of> <!-- print value -->
Run Code Online (Sandbox Code Playgroud)
如果您想知道自己在 for-each 循环中的位置,可以使用内置的 position() 函数。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Collection">
<xsl:for-each select="Content">
<xsl:if test="position() < 3">
<!-- Do this for nodes 1, 2 and 3 -->
</xsl:if>
<!-- Do this for every node -->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66555 次 |
| 最近记录: |