开发者

formatting in xslt

I want to display a set of data in pivoted form

The data retrieved from query is in this format

AccID                Month                Year       FTE

1                     Jan                  2009       10
1                     Feb                  2009      20
2                     Jan                  2010 开发者_开发问答     30               

I want to display it as

AccID  Jan 2009  Feb 2009  Jan 2010       
1        10        20        
2                            30              


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kItemByDate" match="item" use="concat(Month,Year)"/>
    <xsl:key name="kItemByAccID" match="item" use="AccID"/>
    <xsl:variable name="vDates" 
         select="/root/item[count(.|key('kItemByDate',
                                        concat(Month,Year))[1])=1]"/>
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <table>
            <tr>
                <th>AccID</th>
                <xsl:for-each select="$vDates">
                    <th>
                        <xsl:value-of select="concat(Month,' ',Year)"/>
                    </th>
                </xsl:for-each>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="item[count(.|key('kItemByAccID',AccID)[1])=1]">
        <xsl:variable name="vItems" select="key('kItemByAccID',AccID)"/>
        <tr>
            <td>
                <xsl:value-of select="AccID"/>
            </td>
            <xsl:for-each select="$vDates">
                <td>
                    <xsl:value-of select="$vItems[Month=current()/Month]
                                                 [Year=current()/Year]
                                                 /FTE"/>
                </td>
            </xsl:for-each>
        </tr>
    </xsl:template>
</xsl:stylesheet>

With this input:

<root>
    <item>
        <AccID>1</AccID>
        <Month>Jan</Month>
        <Year>2009</Year>
        <FTE>10</FTE>
    </item>
    <item>
        <AccID>1</AccID>
        <Month>Feb</Month>
        <Year>2009</Year>
        <FTE>20</FTE>
    </item>
    <item>
        <AccID>2</AccID>
        <Month>Jan</Month>
        <Year>2010</Year>
        <FTE>30</FTE>
    </item>
</root>

Output:

<table>
    <tr>
        <th>AccID</th>
        <th>Jan 2009</th>
        <th>Feb 2009</th>
        <th>Jan 2010</th>
    </tr>
    <tr>
        <td>1</td>
        <td>10</td>
        <td>20</td>
        <td></td>
    </tr>
    <tr>
        <td>2</td>
        <td></td>
        <td></td>
        <td>30</td>
    </tr>
</table>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜