for-each:获取每个第三项(分成3列) [英] for-each: get every 3rd item (split into 3 columns)
本文介绍了for-each:获取每个第三项(分成3列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在xsl中有以下节点:
< foo>
< bar> 1< / bar>
< bar> 2< / bar>
< bar> 3< / bar>
< bar> 4< / bar>
< bar> 5< / bar>
< bar> 6< / bar>
< bar> 7< / bar>
< bar> 8< / bar>
< bar> 9< / bar>
< / foo>
并且想把它变成下面的html:
< ul class =one>
< li> 1< / li>
< li> 4< / li>
< li> 7< / li>
< / ul>
< ul class =two>
< li> 2< / li>
< li> 5< / li>
< li> 8< / li>
< / ul>
< ul class =three>
< li> 3< / li>
< li> 6< / li>
< li> 9< / li>
< / ul>
如果想要循环和获取每个第三项,这:
< ul class =one>
< li>< xsl:value-of select =node()/>< / li>
< / xsl:for-each>
< / ul>
< ul class =two>
< xsl:for-each select =exlt:node-set($ blah)/ foo / bar [X2]>
< li>< xsl:value-of select =node()/>< / li>
< / xsl:for-each>
< / ul>
< ul class =three>
< li>< xsl:value-of select =node()/>< / li>
< / xsl:for-each>
< / ul>
其中:
X1 =从位置1开始的第三个项目
X2 =从位置2开始的每个第三个项目
X3 =从位置3 $开始的每个第三个项目
可能需要使用last(),但不能完全正确地工作。
解决方案
在XPath中,条件是:
$ b $ pre > not(position()mod 3)
或
position()mod 3 = 0
我看不出为什么你不能使用 op:mod
。
编辑:关于新问题,只需减去偏移量即可。所以:
X1:
position()mod 3 = 1
X2:
< code $ position
position()mod 3 = 0
编辑2 :现在我理解你的问题。I have the following nodes in xsl:
<foo> <bar>1</bar> <bar>2</bar> <bar>3</bar> <bar>4</bar> <bar>5</bar> <bar>6</bar> <bar>7</bar> <bar>8</bar> <bar>9</bar> </foo>
And would like to turn it into the following html:
<ul class="one"> <li>1</li> <li>4</li> <li>7</li> </ul> <ul class="two"> <li>2</li> <li>5</li> <li>8</li> </ul> <ul class="three"> <li>3</li> <li>6</li> <li>9</li> </ul>
Having a hard time figuring out how to loop and get each third item, would like to do something like this:
<ul class="one"> <xsl:for-each select="exlt:node-set($blah)/foo/bar[X1]"> <li><xsl:value-of select="node()"/></li> </xsl:for-each> </ul> <ul class="two"> <xsl:for-each select="exlt:node-set($blah)/foo/bar[X2]"> <li><xsl:value-of select="node()"/></li> </xsl:for-each> </ul> <ul class="three"> <xsl:for-each select="exlt:node-set($blah)/foo/bar[X3]"> <li><xsl:value-of select="node()"/></li> </xsl:for-each> </ul> Where: X1 = Every third item starting from position 1 X2 = Every third item starting from position 2 X3 = Every third item starting from position 3
Might need to use last(), but can't quite get that working correctly.
解决方案In XPath, the condition will be:
not(position() mod 3)
or
position() mod 3 = 0
I don't see why you can't use
op:mod
.EDIT: About new question, just substract the offset. So:
X1:
position() mod 3 = 1
X2:
position() mod 3 = 2
X3:
position() mod 3 = 0
EDIT 2: Now I understand your question.
这篇关于for-each:获取每个第三项(分成3列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文