开发者

Grouping every 3 items in xslt 1.0

I'm having troubles to figure out a way to group items xslt 1.0. I have a source xml similar to the one below:

<client name="client A">
    <project name = "project A1"/>
    <project name = "project A2"/>
    <project name = "project A3"/>
    <project name = "project A4"/>
</client>
<cl开发者_如何学Goient name="client B">
    <project name = "project B1"/>
    <project name = "project B2"/>
</client>
<client name="client C">
    <project name = "project C1"/>
    <project name = "project C2"/>
    <project name = "project C3"/>
</client>

I'd like to select all projects, sort them and then group every 3 project in one boundle as in the example below:

<boundle>
  <project name="project A1">
  <project name="project A2">
  <project name="project A3">
</boundle>
<boundle>
  <project name="project A4">
  <project name="project B1">
  <project name="project B2">
</boundle>
<boundle>
  <project name="project C1">
  <project name="project C2">
  <project name="project C3">
</boundle>

Currently to do so I'm using to open a boundle tag and close it later. Can you think about any better solution?


No grouping necessary.

<xsl:param name="perGroup" select="3" />

<xsl:variable name="allProjects" select="/client/project" />

<xsl:template match="/">
  <xsl:apply-templates select="$allProjects" mode="counted" />
</xsl:template>

<xsl:template match="project" mode="counted">
  <xsl:if test="position() mod $perGroup = 1">
    <xsl:variable name="pos" select="position()" />
    <boundle>
      <xsl:copy-of select="$allProjects[
        position() &gt;= $pos and position() &lt; ($pos + $perGroup)
      ]" />
    </boundle>
  </xsl:template>
</xsl:template>


I can recommend the following link http://www.jenitennison.com/xslt/grouping/ which helped me figuring out how to group various number of elements into some categories based on the contents in the XML document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜