Check out the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:niku="http://www.niku.com/xog/Query">
<xsl:output method="text"/>
<xsl:template match="/">
Got: <xsl:apply-templates />
Expected:
2007,0017016990001001703914F537,2970.0,3505.0,0,0,0,123.0,0,0,0,0,0,0
</xsl:template>
<xsl:template match="niku:QueryResult/niku:Records">
<xsl:for-each-group select="niku:Record"
group-by="concat(@fiscalyear,@gl)">
<xsl:variable name="cg" select="current-group()"/>
<xsl:if test="@fiscalyear != 'null'">
<xsl:value-of select="@fiscalyear"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="@gl"/>
<xsl:text>,</xsl:text>
<xsl:for-each select="1 to 12">
<xsl:variable name="p" select="."/>
<xsl:value-of select="if ($cg[(_at_)month=$p]) then
$cg[(_at_)month=$p]/@cost else 0"/>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
mIchael wolff wrote:
WHat I am trying to do is.... I get an XML result set
that contains dollar values that are asssociated with
a month. I need to put these in a CSV and I need to
have a value for every month. So, if I do not have an
XML node for a certain month, I need to put in a 0.
Here is the code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:niku="http://www.niku.com/xog/Query">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-imports />
</xsl:template>
<xsl:template
match="niku:QueryResult/niku:Code"><xsl:text></xsl:text></xsl:template>
<xsl:template name="monthly" >
<xsl:param name="mon" />
<xsl:variable name="Found">
<xsl:for-each select="current-group()">
<xsl:sort data-type="number" order="ascending"/>
<xsl:choose>
<xsl:when
test="@month=$mon">,<xsl:value-of
select="@cost"/>
<xsl:text>test</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:if test="$Found = ''">
<xsl:text>,0</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="niku:QueryResult/niku:Records">
<xsl:for-each-group select="niku:Record"
group-by="@gl">
<xsl:for-each-group select="current-group()"
group-by="@fiscalyear">
<xsl:if test="@fiscalyear != 'null'">
<xsl:value-of select="@fiscalyear"/>
</xsl:if>
<xsl:text>,</xsl:text>
<xsl:if test="string(@gl) != 'null'">
<xsl:value-of select="@gl"/>
</xsl:if>
<xsl:text>,</xsl:text>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">1</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">2</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">3</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">4</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">5</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">6</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">7</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">8</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">9</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">10</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">11</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="monthly">
<xsl:with-param
name="mon">12</xsl:with-param>
</xsl:call-template>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
AND the XML
<QueryResult xmlns="http://www.niku.com/xog/Query">
<Records>
<Record cost="2970.0" fiscalyear="2007"
gl="0017016990001001703914F537" month="1"/>
fiscalyear="2007"
gl="0017016990001001703912H796"
location="001" month="1" projectclass="12"
projectkey="5000892" rev_notes="null"
version="405"/>
<Record cost="3505.0" fiscalyear="2007"
gl="0017016990001001703914F537" month="2"/>
<Record cost="123.0" fiscalyear="2007"
gl="0017016990001001703914F537" month="6"/>
</Records>
</QueryResult>
The desired output would be:
2007,0017016990001001703914F537,2970.0,3505.0,0,0,0,123.0,0,0,0,0,0,0
____________________________________________________________________________________
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
http://answers.yahoo.com/dir/?link=list&sid=396546091
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--