我正在尝试按字母顺序显示数据,以便以相同字母开头的项目位于不同的列中。在启动新列之前,这些列最多可容纳10个项目。我可以成功地按字母顺序划分数据并将其除以每列的项目数,但我很难合并2:
按字母顺序划分:
<xsl:template match="/">
<xsl:key name="node-by-first-letter" match="node" use="substring(@email, 1, 1)" />
<div class="scroller-panel">
<xsl:for-each select="msxml:node-set($members)/node[count(. | key('node-by-first-letter', substring(@email, 1, 1))[1]) = 1]">
<xsl:sort select="@email" order="ascending"/>
<xsl:apply-templates select="." mode="group" />
</xsl:for-each></div></xsl:template>
<xsl:template match="node" mode="group">
<div class="column-312 scroller-item people-search-column fade-panel">
<h2>
<xsl:value-of select="Exslt.ExsltStrings:uppercase(substring(@email,1,1))"/>
</h2>
<ul class="stripe-list">
<xsl:apply-templates select="key('node-by-first-letter', substring(@email, 1, 1))" mode="item">
<xsl:sort select="@email" />
</xsl:apply-templates>
</ul>
</div>
</xsl:template>
<xsl:template match="node" mode="item">
<li>
<a href="4.0.1.person.profile.html">
<xsl:value-of select="@email"/>
</a>
</li>
</xsl:template>
除以每列的最大项目数:
<xsl:for-each select="msxml:node-set($members)/members/member[position() mod 10 = 1]">
<ul>
<xsl:for-each select=". | following-sibling::*[not(position() >= 10)]">
<li>
<xsl:value-of select="@email"/>
</li>
</xsl:for-each>
</ul>
</xsl:for-each>
首选输出如下:
http://rookery9.aviary.com.s3.amazonaws.com/9676500/9676792_3580_625x625.jpg
答案 0 :(得分:4)
<强>予。 XSLT 2.0解决方案:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pColLength" as="xs:integer" select="10"/>
<xsl:template match="/*">
<names>
<xsl:for-each-group select="name"
group-by="substring(.,1,1)">
<xsl:sort select="current-grouping-key()"/>
<xsl:for-each-group select="current-group()"
group-by="(position()-1) idiv $pColLength">
<column>
<xsl:copy-of select="current-group()"/>
</column>
</xsl:for-each-group>
</xsl:for-each-group>
</names>
</xsl:template>
</xsl:stylesheet>
应用于此XML文档(因为问题中没有提供!!!):
<names>
<name>T A</name>
<name>T B</name>
<name>T C</name>
<name>T D</name>
<name>T E</name>
<name>T F</name>
<name>T G</name>
<name>T H</name>
<name>T I</name>
<name>T J</name>
<name>T K</name>
<name>T L</name>
<name>A A</name>
<name>A B</name>
<name>A C</name>
<name>A D</name>
<name>A E</name>
<name>A F</name>
<name>A G</name>
<name>A H</name>
<name>A I</name>
<name>A J</name>
<name>A K</name>
<name>A L</name>
<name>X A</name>
<name>X B</name>
<name>X C</name>
<name>X D</name>
<name>X E</name>
<name>X F</name>
<name>X G</name>
<name>X H</name>
<name>X I</name>
<name>X J</name>
<name>X K</name>
<name>X L</name>
<name>R A</name>
<name>R B</name>
<name>R C</name>
<name>R D</name>
<name>R E</name>
<name>R F</name>
<name>R G</name>
<name>R H</name>
<name>R I</name>
<name>R J</name>
<name>R K</name>
<name>R L</name>
</names>
生成所需的输出 - 按开头第一个字母排序的名称,并将其分为10个项目的列:
<names>
<column>
<name>A A</name>
<name>A B</name>
<name>A C</name>
<name>A D</name>
<name>A E</name>
<name>A F</name>
<name>A G</name>
<name>A H</name>
<name>A I</name>
<name>A J</name>
</column>
<column>
<name>A K</name>
<name>A L</name>
</column>
<column>
<name>R A</name>
<name>R B</name>
<name>R C</name>
<name>R D</name>
<name>R E</name>
<name>R F</name>
<name>R G</name>
<name>R H</name>
<name>R I</name>
<name>R J</name>
</column>
<column>
<name>R K</name>
<name>R L</name>
</column>
<column>
<name>T A</name>
<name>T B</name>
<name>T C</name>
<name>T D</name>
<name>T E</name>
<name>T F</name>
<name>T G</name>
<name>T H</name>
<name>T I</name>
<name>T J</name>
</column>
<column>
<name>T K</name>
<name>T L</name>
</column>
<column>
<name>X A</name>
<name>X B</name>
<name>X C</name>
<name>X D</name>
<name>X E</name>
<name>X F</name>
<name>X G</name>
<name>X H</name>
<name>X I</name>
<name>X J</name>
</column>
<column>
<name>X K</name>
<name>X L</name>
</column>
</names>
<强>解释强>:
嵌套xsl:for-each-group
- 首先按起始字符分组,然后针对每个此类已确定和已排序的组 - 按其项目所在的列数排序
使用标准XSLT 2.0函数current-grouping-key()
和current-group()
。
II.XSLT 1.0解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pColLength" select="10"/>
<xsl:key name="kStarting" match="name"
use="substring(.,1,1)"/>
<xsl:template match="/*">
<names>
<xsl:for-each select=
"name
[generate-id()
=
generate-id(key('kStarting', substring(.,1,1))[1])
]
">
<xsl:sort select="substring(.,1,1)"/>
<xsl:variable name="vgroupNames" select=
"key('kStarting', substring(.,1,1))"/>
<xsl:apply-templates select="$vgroupNames[1]">
<xsl:with-param name="pGroup" select="$vgroupNames"/>
<xsl:with-param name="pGroupLength" select=
"count($vgroupNames)"/>
</xsl:apply-templates>
</xsl:for-each>
</names>
</xsl:template>
<xsl:template match="name">
<xsl:param name="pGroup"/>
<xsl:param name="pGroupLength"/>
<xsl:param name="pInd" select="1"/>
<xsl:if test="not($pInd > $pGroupLength)">
<column>
<xsl:copy-of select=
"$pGroup
[position() >= $pInd
and
not(position() > $pInd + $pColLength -1)
]"/>
</column>
<xsl:apply-templates select=
"$pGroup[position() = $pInd + $pColLength]">
<xsl:with-param name="pGroup" select="$pGroup"/>
<xsl:with-param name="pGroupLength" select="$pGroupLength"/>
<xsl:with-param name="pInd" select="$pInd + $pColLength"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
应用于同一XML文档(如上所述)时,生成相同的所需输出 - 按开头第一个字母排序的名称,并放入10个项目的列中:
<names>
<column>
<name>A A</name>
<name>A B</name>
<name>A C</name>
<name>A D</name>
<name>A E</name>
<name>A F</name>
<name>A G</name>
<name>A H</name>
<name>A I</name>
<name>A J</name>
</column>
<column>
<name>A K</name>
<name>A L</name>
</column>
<column>
<name>R A</name>
<name>R B</name>
<name>R C</name>
<name>R D</name>
<name>R E</name>
<name>R F</name>
<name>R G</name>
<name>R H</name>
<name>R I</name>
<name>R J</name>
</column>
<column>
<name>R K</name>
<name>R L</name>
</column>
<column>
<name>T A</name>
<name>T B</name>
<name>T C</name>
<name>T D</name>
<name>T E</name>
<name>T F</name>
<name>T G</name>
<name>T H</name>
<name>T I</name>
<name>T J</name>
</column>
<column>
<name>T K</name>
<name>T L</name>
</column>
<column>
<name>X A</name>
<name>X B</name>
<name>X C</name>
<name>X D</name>
<name>X E</name>
<name>X F</name>
<name>X G</name>
<name>X H</name>
<name>X I</name>
<name>X J</name>
</column>
<column>
<name>X K</name>
<name>X L</name>
</column>
</names>
<强>解释强>:
使用Muenchian grouping method ,加上排序,我们获得(按排序顺序)每组name
元素,这些元素由以相同字符开头的所有名称组成。
通过将模板应用于其第一个name
元素来处理上面获得的每组name
个元素。整个组,其长度和组中name
元素的索引(默认值= 1)将作为参数传递。
保证匹配name
元素的模板仅应用于列中的起始元素。它创建一个新的column
元素,并在其中复制此列的所有name
元素(从索引$pInd
开始,到索引$pInd+$pColLength -1
结束。不要求这些元素应该是兄弟姐妹(并且它们不是)。如果不仅仅是复制而且每个name
还需要额外的处理,这可以通过将<xsl:copy-of>
指令替换为:
-
<xsl:apply-templates mode="process" select=
"$pGroup
[position() >= $pInd
and
not(position() > $pInd + $pColLength -1)
]"/>