SharePoint Designer数据视图 - XSLT计数

时间:2012-06-21 15:22:18

标签: sharepoint xslt sharepoint-2010 dataviewwebpart

我正在使用SPD 2010中的数据视图Web部件。我的xml结构如下:

<ProjectGroups>
    <ProjectGroup>
        <GroupID>1</GroupID>
        <ProjectName>Project 1</ProjectName>
    </ProjectGroup>
    <ProjectGroup>
        <GroupID>2</GroupID>
        <ProjectName>Project 2</ProjectName>
    </ProjectGroup>
    <ProjectGroup>
        <GroupID>2</GroupID>
        <ProjectName>Project 3</ProjectName>
    </ProjectGroup>
    </ProjectGroups>

这是一个汇总网页部分,所以我要做的是获取每个项目组下的项目计数。对于我上面的例子,Group ID 1有1个项目,Group ID 2有2个。我相信有办法做到这一点,但我有点学习xslt,所以我不确定该做什么。任何帮助表示赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

此样式表......

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="groups" match="ProjectGroup" use="GroupID" />

<xsl:template match="@*|node()">
 <xsl:copy> 
   <xsl:apply-templates select="@*|node()" />
 </xsl:copy> 
</xsl:template>

<xsl:template match="ProjectGroup[generate-id()=generate-id(key('groups',GroupID)[1])]">
 <xsl:copy> 
   <xsl:apply-templates select="@*" />
   <xsl:attribute name="count-of-projects">
     <xsl:value-of select="count(key('groups',GroupID))" />
   </xsl:attribute>
   <xsl:apply-templates select="node()" />
 </xsl:copy> 
</xsl:template>

</xsl:stylesheet>

...当应用于您的输入时,将产生......

<?xml version="1.0" encoding="utf-8"?>
<ProjectGroups>
    <ProjectGroup count-of-projects="1">
        <GroupID>1</GroupID>
        <ProjectName>Project 1</ProjectName>
    </ProjectGroup>
    <ProjectGroup count-of-projects="2">
        <GroupID>2</GroupID>
        <ProjectName>Project 2</ProjectName>
    </ProjectGroup>
    <ProjectGroup>
        <GroupID>2</GroupID>
        <ProjectName>Project 3</ProjectName>
    </ProjectGroup>
</ProjectGroups>