我遇到的问题是我想要求的字段在XSL中连接起来。我已经尝试了以下线程中给出的解决方案,但它们都没有为我工作。要求是为Report创建excel模板,其中求和字段需要使用IF条件执行,即当PEOPLEGROUP在'CF1000'或'CF1100'时,则输出应该是HEADCOUNT字段的总和但由于某种原因输出显示并排而是总和。
下面给出的是示例代码,预期值为508,但它给了我193315.
示例数据:
<?xml version="1.0" encoding="UTF-8"?>
<DATA_DS>
<G_1>
<PG_BUSINESSUNITNAME>Conventional</PG_BUSINESSUNITNAME>
<G_2>
<PG_DEPARTMENTLEVEL1>Conventional Field</PG_DEPARTMENTLEVEL1>
<G_3>
<PG_DEPARTMENTLEVEL2>Conventional Field Operations - Reporting</PG_DEPARTMENTLEVEL2>
<G_4>
<S_0>0</S_0>
<PG>00001.SVOPR</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>SVOPR</PEOPLEGROUP>
<HEADCOUNT>3.0</HEADCOUNT>
</G_4>
</G_3>
<G_3>
<PG_DEPARTMENTLEVEL2>Field Operations</PG_DEPARTMENTLEVEL2>
<G_4>
<S_0>0</S_0>
<PG>00001.CF1000</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>CF1000</PEOPLEGROUP>
<HEADCOUNT>193.0</HEADCOUNT>
</G_4>
<G_4>
<S_0>0</S_0>
<PG>00001.CF1100</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>CF1100</PEOPLEGROUP>
<HEADCOUNT>315.0</HEADCOUNT>
</G_4>
<G_4>
<S_0>0</S_0>
<PG>00001.CF1200</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>CF1200</PEOPLEGROUP>
<HEADCOUNT>23.0</HEADCOUNT>
</G_4>
</G_3>
</G_2>
</G_1>
</DATA_DS>
示例代码如下所示
<xsl:for-each select=".//G_4">
<xsl:variable name="ALB" select="'CF1000 CF1100'" />
<xsl:variable name="PGALB" select="PEOPLEGROUP" />
<xsl:if test="contains(concat(' ', $ALB, ' '),concat(' ',$PGALB, ' '))">
<xsl:value-of select="sum(HEADCOUNT)" />
</xsl:if>
</xsl:for-each>
任何帮助将不胜感激。
答案 0 :(得分:1)
就像@ jim-garrison在评论中所说的那样,上下文是G_4
,每个G_4
只有一个HEADCOUNT
所以没有任何东西可以求和。您只需获得每个单独值的串联。
相反,请考虑创建一个xsl:key,然后在密钥的HEADCOUNT
上进行求和。
示例...
XML输入
<DATA_DS>
<G_1>
<PG_BUSINESSUNITNAME>Conventional</PG_BUSINESSUNITNAME>
<G_2>
<PG_DEPARTMENTLEVEL1>Conventional Field</PG_DEPARTMENTLEVEL1>
<G_3>
<PG_DEPARTMENTLEVEL2>Conventional Field Operations - Reporting</PG_DEPARTMENTLEVEL2>
<G_4>
<S_0>0</S_0>
<PG>00001.SVOPR</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>SVOPR</PEOPLEGROUP>
<HEADCOUNT>3.0</HEADCOUNT>
</G_4>
</G_3>
<G_3>
<PG_DEPARTMENTLEVEL2>Field Operations</PG_DEPARTMENTLEVEL2>
<G_4>
<S_0>0</S_0>
<PG>00001.CF1000</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>CF1000</PEOPLEGROUP>
<HEADCOUNT>193.0</HEADCOUNT>
</G_4>
<G_4>
<S_0>0</S_0>
<PG>00001.CF1100</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>CF1100</PEOPLEGROUP>
<HEADCOUNT>315.0</HEADCOUNT>
</G_4>
<G_4>
<S_0>0</S_0>
<PG>00001.CF1200</PG>
<S_5>3.00000001521557E14</S_5>
<PEOPLEGROUP>CF1200</PEOPLEGROUP>
<HEADCOUNT>23.0</HEADCOUNT>
</G_4>
</G_3>
</G_2>
</G_1>
</DATA_DS>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="headcount"
match="G_4[contains(concat(' ','CF1000 CF1100',' '),concat(' ',PEOPLEGROUP,' '))]" use="generate-id(ancestor::G_3)"/>
<xsl:template match="/*">
<xsl:for-each select="//G_3">
<sum><xsl:value-of select="sum(key('headcount',generate-id(current()))/HEADCOUNT)"/></sum>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<sum>0</sum>
<sum>508</sum>
我之前从未尝试过在Excel模板中使用XSLT,所以我不确定这些限制是什么。也许尝试更简单的事情(这将取代现有的xsl:for-each
):
<xsl:for-each select="//G_3">
<xsl:variable name="ALB" select="'CF1000 CF1100'" />
<sum>
<xsl:value-of select="sum(.//G_4[contains(concat(' ', $ALB, ' '),concat(' ',PEOPLEGROUP, ' '))]/HEADCOUNT)"/>
</sum>
</xsl:for-each>