Col*_*gan 4 xslt xpath symphony-cms xslt-1.0
所以我试图解决xslt中的一个问题,我通常会知道如何用命令式语言做.我正在从一个xml元素列表中添加单元格,标准的东西.所以:
<some-elements>
<element>"the"</element>
<element>"minds"</element>
<element>"of"</element>
<element>"Douglas"</element>
<element>"Hofstadter"</element>
<element>"and"</element>
<element>"Luciano"</element>
<element>"Berio"</element>
</some-elements>
Run Code Online (Sandbox Code Playgroud)
但是,我希望在达到某个字符最大值后切断一行并开始一个新行.所以说我最多允许每行20个字符.我最终会这样:
<table>
<tr>
<td>"the"</td>
<td>"minds"</td>
<td>"of"</td>
<td>"Douglas"</td>
</tr>
<tr>
<td>"Hofstadter"</td>
<td>"and"</td>
<td>"Luciano"</td>
</tr>
<tr>
<td>"Berio"</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
在命令式语言中,我将元素追加到一行,同时将每个元素string-count添加到一些可变变量中.当该变量超过20时,我将停止,构建一个新行,并在将字符串计数返回到零之后重新运行该行上的整个过程(从停止的元素开始).但是,我无法在XSLT中更改变量值.这整个无状态的功能评估事件让我陷入了困境.
从xsl-list来到这个论坛就像回到10年,为什么每个人都使用xslt 1 :-)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="some-elements">
<table>
<xsl:apply-templates select="element[1]"/>
</table>
</xsl:template>
<xsl:template match="element">
<xsl:param name="row"/>
<xsl:choose>
<xsl:when test="(string-length($row)+string-length(.))>20
or
not(following-sibling::element[1])">
<tr>
<xsl:copy-of select="$row"/>
<xsl:copy-of select="."/>
</tr>
<xsl:apply-templates select="following-sibling::element[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="following-sibling::element[1]">
<xsl:with-param name="row">
<xsl:copy-of select="$row"/>
<xsl:copy-of select="."/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1137 次 |
| 最近记录: |