使用 XSL 和 XML 在 PHP 中通过循环添加值
Addind values over loop in FOP with XSL and XML
我有这个 XML 和 XSL 示例:
XML:
<mastercount>
<sourceval>
<attm>50</attm>
<fh>6500</fh>
<id>1</id>
</sourceval>
<sourceval>
<attm>15</attm>
<fh>2300</fh>
<id>2</id>
</sourceval>
<sourceval>
<attm>4</attm>
<fh>280</fh>
<id>3</id>
</sourceval>
<sourceval>
<attm>20</attm>
<fh>2700</fh>
<id>4</id>
</sourceval>
</mastercount>
XSL:
<xsl:variable name="var_idx">
<xsl:value-of select="position()" />
</xsl:variable>
<xsl:variable name="var_sum_attm" />
<xsl:variable name="var_sum_fh" />
<fo:table-row>
<xsl:for-each select="/mastercount">
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sourceval[position()=$var_idx]/attm" />
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sourceval[position()=$var_idx]/fh" />
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
源代码的这一部分效果很好,我得到了
attm fh attm fh
Val2 50 6500 0 6500
Val1 15 2300 0 2300
Val3 280 0 280 0
Val4 20 2700 0 2700
(我在源代码中跳过了上面的名称部分)
但是我现在需要这 2 个字段的总和:
attm fh attm fh
Val2 50 6500 0 6500
Val1 15 2300 0 2300
Val3 280 0 280 0
Val4 20 2700 0 2700
sum sum sum sum
我怎样才能让它工作?
有什么想法吗?
感谢您的帮助。
流浪汉
更新:
感谢 RT72 这是一个答案:
<xsl:for-each select="/mastercount">
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(sourceval/attm)"/>
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(sourceval/fh)" />
</fo:block>
</fo:table-cell>
</xsl:for-each>
以下应该适合您:
<xsl:variable name="var_sum_attm" select="sum(/mastercount/sourceval/attm)"/>
<xsl:variable name="var_sum_fh" select="sum(/mastercount/sourceval/fh)"/>
在 OP 编辑问题后更新
试试这个
<xsl:for-each select="/mastercount/sourceval">
<fo:table-row>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="attm" />
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="fh" />
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(/mastercount/sourceval/attm)" />
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(/mastercount/sourceval/fh)" />
</fo:block>
</fo:table-cell>
</fo:table-row>
你也可以用这个
<xsl:value-of select="sum(for $i in //attm return $i)"/>
<xsl:value-of select="sum(for $i in //fh return $i)"/>
使用反映源代码结构并让 XSLT 处理器确定要处理的内容的 xsl:template
会更简单。下面的示例在 mastercount
的模板中进行求和,sourceval
的模板以及 attm
和 fh
生成它们各自的 FO:
<xsl:attribute-set name="table-cell">
<xsl:attribute name="padding">3pt</xsl:attribute>
<xsl:attribute name="border-style">solid</xsl:attribute>
<xsl:attribute name="width">12mm</xsl:attribute>
<xsl:attribute name="border-width">1pt</xsl:attribute>
<xsl:attribute name="text-align">right</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="mastercount">
<fo:table>
<fo:table-body>
<xsl:apply-templates />
<fo:table-row>
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:value-of select="sum(sourceval/attm)" />
</fo:block>
</fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:value-of select="sum(sourceval/fh)" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="sourceval">
<fo:table-row>
<xsl:apply-templates select="attm | fh"/>
</fo:table-row>
</xsl:template>
<xsl:template match="attm | fh">
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:table-cell>
</xsl:template>
如果您只想使用一个模板生成具有所需属性的 fo:table-cell
,您可以将 mastercount
、attm
和 fh
的模板更改为:
<xsl:template match="mastercount">
<fo:table>
<fo:table-body>
<xsl:apply-templates />
<fo:table-row>
<xsl:call-template name="table-cell">
<xsl:with-param name="value" select="sum(sourceval/attm)" />
</xsl:call-template>
<xsl:call-template name="table-cell">
<xsl:with-param name="value" select="sum(sourceval/fh)" />
</xsl:call-template>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="attm | fh" name="table-cell">
<xsl:param name="value" select="." />
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:value-of select="$value" />
</fo:block>
</fo:table-cell>
</xsl:template>
我有这个 XML 和 XSL 示例:
XML:
<mastercount>
<sourceval>
<attm>50</attm>
<fh>6500</fh>
<id>1</id>
</sourceval>
<sourceval>
<attm>15</attm>
<fh>2300</fh>
<id>2</id>
</sourceval>
<sourceval>
<attm>4</attm>
<fh>280</fh>
<id>3</id>
</sourceval>
<sourceval>
<attm>20</attm>
<fh>2700</fh>
<id>4</id>
</sourceval>
</mastercount>
XSL:
<xsl:variable name="var_idx">
<xsl:value-of select="position()" />
</xsl:variable>
<xsl:variable name="var_sum_attm" />
<xsl:variable name="var_sum_fh" />
<fo:table-row>
<xsl:for-each select="/mastercount">
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sourceval[position()=$var_idx]/attm" />
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sourceval[position()=$var_idx]/fh" />
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
源代码的这一部分效果很好,我得到了
attm fh attm fh
Val2 50 6500 0 6500
Val1 15 2300 0 2300
Val3 280 0 280 0
Val4 20 2700 0 2700
(我在源代码中跳过了上面的名称部分)
但是我现在需要这 2 个字段的总和:
attm fh attm fh
Val2 50 6500 0 6500
Val1 15 2300 0 2300
Val3 280 0 280 0
Val4 20 2700 0 2700
sum sum sum sum
我怎样才能让它工作?
有什么想法吗?
感谢您的帮助。 流浪汉
更新:
感谢 RT72 这是一个答案:
<xsl:for-each select="/mastercount">
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(sourceval/attm)"/>
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(sourceval/fh)" />
</fo:block>
</fo:table-cell>
</xsl:for-each>
以下应该适合您:
<xsl:variable name="var_sum_attm" select="sum(/mastercount/sourceval/attm)"/>
<xsl:variable name="var_sum_fh" select="sum(/mastercount/sourceval/fh)"/>
在 OP 编辑问题后更新
试试这个
<xsl:for-each select="/mastercount/sourceval">
<fo:table-row>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="attm" />
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="fh" />
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(/mastercount/sourceval/attm)" />
</fo:block>
</fo:table-cell>
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
<fo:block>
<xsl:value-of select="sum(/mastercount/sourceval/fh)" />
</fo:block>
</fo:table-cell>
</fo:table-row>
你也可以用这个
<xsl:value-of select="sum(for $i in //attm return $i)"/>
<xsl:value-of select="sum(for $i in //fh return $i)"/>
使用反映源代码结构并让 XSLT 处理器确定要处理的内容的 xsl:template
会更简单。下面的示例在 mastercount
的模板中进行求和,sourceval
的模板以及 attm
和 fh
生成它们各自的 FO:
<xsl:attribute-set name="table-cell">
<xsl:attribute name="padding">3pt</xsl:attribute>
<xsl:attribute name="border-style">solid</xsl:attribute>
<xsl:attribute name="width">12mm</xsl:attribute>
<xsl:attribute name="border-width">1pt</xsl:attribute>
<xsl:attribute name="text-align">right</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="mastercount">
<fo:table>
<fo:table-body>
<xsl:apply-templates />
<fo:table-row>
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:value-of select="sum(sourceval/attm)" />
</fo:block>
</fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:value-of select="sum(sourceval/fh)" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="sourceval">
<fo:table-row>
<xsl:apply-templates select="attm | fh"/>
</fo:table-row>
</xsl:template>
<xsl:template match="attm | fh">
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:table-cell>
</xsl:template>
如果您只想使用一个模板生成具有所需属性的 fo:table-cell
,您可以将 mastercount
、attm
和 fh
的模板更改为:
<xsl:template match="mastercount">
<fo:table>
<fo:table-body>
<xsl:apply-templates />
<fo:table-row>
<xsl:call-template name="table-cell">
<xsl:with-param name="value" select="sum(sourceval/attm)" />
</xsl:call-template>
<xsl:call-template name="table-cell">
<xsl:with-param name="value" select="sum(sourceval/fh)" />
</xsl:call-template>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="attm | fh" name="table-cell">
<xsl:param name="value" select="." />
<fo:table-cell xsl:use-attribute-sets="table-cell">
<fo:block>
<xsl:value-of select="$value" />
</fo:block>
</fo:table-cell>
</xsl:template>