XSLT全局计数的分组项目

时间:2009-12-14 16:06:43

标签: xslt sorting count

我有一组项目,我使用muenchian方法使用键进行分组。这很有效,但是当我尝试使用前x个项目时,它会在每个组中的x个项目上执行,而不是在整个结果集中执行。我如何获得整个集合中每个项目的个人位置?

    <xsl:key name="pictures-by-productid" match="/dsQueryResponse/Rows/Row" use="@ProductId" />


<xsl:template match="/">
    <div style="border:1px solid red; float:left;">
        <xsl:apply-templates select="/" mode="sub">

        </xsl:apply-templates>
    </div>
</xsl:template>

和第二个模板

    <xsl:template match="/" mode="sub">    <xsl:for-each select="/dsQueryResponse/Rows/Row[count(. | key('pictures-by-productid', @ProductId)[1]) = 1]">
        <xsl:for-each select="key('pictures-by-productid', @ProductId)">            
            <xsl:sort select="@PictureType" />                  
            <div style="float:left; margin:2px;">
            <img src="{@ThumbNailUrl}" width="58" /> <br />             
            Download            
            <xsl:number value="position()" format="1. " />
            <xsl:value-of select="." />
            </div>

        </xsl:for-each>
    </xsl:for-each>
</xsl:template> 

1 个答案:

答案 0 :(得分:0)

<xsl:key 
  name="pictures-by-productid" 
  match="/dsQueryResponse/Rows/Row" 
  use="@ProductId"
/>

<xsl:template match="/">
  <div style="border:1px solid red; float:left;">
    <!-- iterate the group, sorted by PictureType… -->
    <xsl:for-each select="/dsQueryResponse/Rows/Row[
      count(. | key('pictures-by-productid', @ProductId)[1]) = 1
    ]">
      <xsl:sort select="@PictureType">
      <!-- …and output only if among the fist 6 items -->
      <xsl:if test="position() &lt;= 6">
        <xsl:apply-templates 
          mode="picture"
          select="key('pictures-by-productid', @ProductId)" 
        />
      </xsl:if>
    </xsl:for-each>
  </div>
</xsl:template>

<xsl:template match="/dsQueryResponse/Rows/Row" mode="picture">
  <div style="float:left; margin:2px;">
    <img src="{@ThumbNailUrl}" width="58" /> <br />                         
    <xsl:text>Download </xsl:text>
    <xsl:number value="position()" format="1. " />
    <xsl:value-of select="." />
  </div>
</xsl:template>
相关问题