指定计数后如何在xsl中断开表行? [英] How can I break a table row in xsl after a specified count?
问题描述
我有以下xsl,可按字母顺序对xml进行排序:
I have the following xsl that sorts my xml alphabetically:
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:key name="rows-by-title" match="Row" use="translate(substring(@Title,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
<xsl:variable name="StartRow" select="string('<tr >')" />
<xsl:template name="Meunchian" match="/dsQueryResponse/Rows">
<table>
<tr>
<xsl:for-each select="Row[count(. | key('rows-by-title', translate(substring(@Title,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'))[1]) = 1]">
<xsl:sort select="translate(substring(@Title,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
<!-- Puts out the title -->
<td>
<xsl:value-of select="translate(substring(@Title,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
</td>
<!-- Now all it's children -->
<xsl:for-each select="key('rows-by-title', translate(substring(@Title,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'))">
<xsl:value-of select="@Title" /><br/>
</xsl:for-each>
</xsl:for-each>
</tr>
</table>
</xsl:template>
XML:
<dsQueryResponse>
<Rows>
<Row Title="Agenda" />
<Row Title="Policy" />
<Row Title="Policy" />
<Row Title="Report" />
<Row Title="Report" />
</Rows>
</dsQueryResponse>
我现在想每输出4列就中断一次表格行,以使输出看起来像这样:
I now want to break the table row every 4 columns that are output so that the output looks something like:
ABCD
EFGH
IJKL
MNOP
QRST
UVWX
YZ
有人可以建议实现这一目标的最佳方法吗?
Can anyone suggest the best way to achieve this?
非常感谢
推荐答案
这是我的解决方案.
您可以通过参数"per-row"
和"show-empty"
决定是否要显示空单元格还是要隐藏它们.我确定存在一个更优雅的版本,但是我无法提出一个. ;-)欢迎发表评论.
You can decide via parameters "per-row"
and "show-empty"
if you want empty cells to show up or if you want to hide them. I'm sure a much more elegant version exists, but I could not come up with one. ;-) Comments welcome.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>
<xsl:key name="rows-by-title" match="Row" use="translate(substring(@Title, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
<xsl:variable name="alphabet" select="string('ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
<xsl:variable name="per-row" select="number(4)" />
<xsl:variable name="show-empty" select="false()" />
<xsl:template match="/">
<xsl:apply-templates select="dsQueryResponse/Rows" />
</xsl:template>
<xsl:template match="Rows">
<table>
<xsl:call-template name="create-rows" />
</table>
</xsl:template>
<xsl:template name="create-rows">
<xsl:param name="index" select="1" />
<xsl:variable name="letters">
<xsl:call-template name="next-letters">
<xsl:with-param name="index" select="$index" />
</xsl:call-template>
</xsl:variable>
<xsl:if test="$letters != ''">
<tr title="{$letters}">
<xsl:call-template name="create-cells">
<xsl:with-param name="letters" select="$letters" />
</xsl:call-template>
</tr>
</xsl:if>
<xsl:if test="string-length($letters) = $per-row">
<xsl:call-template name="create-rows">
<xsl:with-param name="index" select="string-length(substring-before($alphabet, substring($letters, string-length($letters), 1))) + 2" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="next-letters">
<xsl:param name="index" />
<xsl:variable name="letter" select="substring($alphabet, $index, 1)" />
<xsl:variable name="letters">
<xsl:if test="$index <= string-length($alphabet)">
<xsl:if test="$show-empty or key('rows-by-title', $letter)">
<xsl:value-of select="$letter" />
</xsl:if>
<xsl:call-template name="next-letters">
<xsl:with-param name="index" select="$index + 1" />
</xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:value-of select="substring($letters, 1, $per-row)" />
</xsl:template>
<xsl:template name="create-cells">
<xsl:param name="letters" />
<xsl:variable name="letter" select="substring($letters, 1, 1)" />
<xsl:if test="$letter != ''">
<td title="{$letter}">
<strong>
<xsl:value-of select="$letter" />
</strong>
<xsl:apply-templates select="key('rows-by-title', $letter)">
<xsl:sort select="@Title" />
</xsl:apply-templates>
</td>
<xsl:call-template name="create-cells">
<xsl:with-param name="letters" select="substring($letters, 2, string-length($letters) - 1)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="Row">
<br />
<xsl:value-of select="@Title" />
</xsl:template>
</xsl:stylesheet>
使用此输入:
<dsQueryResponse>
<Rows>
<Row Title="Agenda" />
<Row Title="Policy" />
<Row Title="Policy" />
<Row Title="Report" />
<Row Title="Report" />
<Row Title="Test2" />
<Row Title="Test1" />
<Row Title="Boo" />
<Row Title="Foo" />
</Rows>
</dsQueryResponse>
产生此输出(title
属性仅用于调试.我将其留在其中,可随时将其删除):
This output is produced (the title
attributes were just for debugging. I left them in, remove them anytime):
<table>
<tr title="ABFP">
<td title="A">
<strong>A</strong>
<br>Agenda
</td>
<td title="B">
<strong>B</strong>
<br>Boo
</td>
<td title="F">
<strong>F</strong>
<br>Foo
</td>
<td title="P">
<strong>P</strong>
<br>Policy
<br>Policy
</td>
</tr>
<tr title="RT">
<td title="R">
<strong>R</strong>
<br>Report
<br>Report
</td>
<td title="T">
<strong>T</strong>
<br>Test1
<br>Test2
</td>
</tr>
</table>
这篇关于指定计数后如何在xsl中断开表行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!