XSLT总计和小计

时间:2011-07-23 09:05:31

标签: html xml xslt xpath

这是我的XML:

<Forms>
  <Form PRONME="Air Prospect 2" PPRONME="0" TBONUSP="1000" NACRES="2" />
  <Form PRONME="Air Prospect 2" PPRONME="0" TBONUSP="1000" NACRES="2" />
  <Form PRONME="Air Prospect 2" PPRONME="0" TBONUSP="1000" NACRES="2" LDATE="Jul" />
  <Form PRONME="Air Prospect Name" PPRONME="0" NACRES="1" />
  <Form PRONME="Air Prospect Name" PPRONME="0" TBONUSP="1000" NACRES="1" />
  <Total PRONME="Air Prospect 2" PPRONME="0" TBONUSP="3000"  NACRES="6" />
  <Total PRONME="Air Prospect Name" PPRONME="0" NACRES="2" TBONUSP="1000"  />
</Forms>

我希望将表单元素打印到一个组中,之后我要打印该组的总计,例如:

<Form PRONME="Air Prospect 2" PPRONME="0" TBONUSP="1000" NACRES="2" />
<Form PRONME="Air Prospect 2" PPRONME="0" TBONUSP="1000" NACRES="2" />
<Form PRONME="Air Prospect 2" PPRONME="0" TBONUSP="1000" NACRES="2" LDATE="Jul" />

读完这个组后我想打印

<Total PRONME="Air Prospect 2" PPRONME="0" TBONUSP="3000"  NACRES="6" />

基于PRONME我想组... 使用XSLT 1.0

2 个答案:

答案 0 :(得分:3)

要以HTML格式显示,您可以使用此模板:

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

  <xsl:key name="k" match="Form" use="@PRONME"/>

  <xsl:template match="/">
    <html>
      <head>
        <style type="text/css">td,th{border:1px solid black}</style>
      </head>
      <table style="border:solid 1px black;border-collapse:collapse">
        <tr>
          <th>PRONME</th>
          <th>PPRONME</th>
          <th>TBONUSP</th>
          <th>NACRES</th>
        </tr>
        <xsl:apply-templates select="*/Form[generate-id(.) = generate-id(key('k', @PRONME))]"/>
      </table>
    </html>
  </xsl:template>

  <xsl:template match="Form">
    <tr>
      <td>
        <xsl:value-of select="@PRONME"/>
      </td>
      <td>
        <xsl:value-of select="@PPRONME"/>
      </td>
      <td>
        <xsl:value-of select="sum(//Form[@PRONME = current()/@PRONME]/@TBONUSP)"/>
      </td>
      <td>
        <xsl:value-of select="sum(//Form[@PRONME = current()/@PRONME]/@NACRES)"/>
      </td>
    </tr>

  </xsl:template>

</xsl:stylesheet>

输出:

enter image description here

XSLT:

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

  <xsl:template match="/">
    <html>
      <head>
        <style type="text/css">td,th{border:1px solid black}</style>
      </head>
      <table style="border:solid 1px black;border-collapse:collapse">
        <tr>
          <th>PRONME</th>
          <th>PPRONME</th>
          <th>TBONUSP</th>
          <th>NACRES</th>
        </tr>
        <xsl:apply-templates select="*/Total"/>
      </table>
    </html>
  </xsl:template>

  <xsl:template match="Total">
    <xsl:apply-templates select="//Form[@PRONME = current()/@PRONME]"/>

    <tr style="color:red">
      <td>
        <xsl:value-of select="@PRONME"/>
      </td>
      <td>
        <xsl:value-of select="@PPRONME"/>
      </td>
      <td>
        <xsl:value-of select="@TBONUSP"/>
      </td>
      <td>
        <xsl:value-of select="@NACRES"/>
      </td>
    </tr>
  </xsl:template>


  <xsl:template match="Form">

      <tr>
        <td>
          <xsl:value-of select="@PRONME"/>
        </td>
        <td>
          <xsl:value-of select="@PPRONME"/>
        </td>
        <td>
          <xsl:value-of select="@TBONUSP"/>
        </td>
        <td>
          <xsl:value-of select="@NACRES"/>
        </td>
      </tr>

  </xsl:template>

</xsl:stylesheet>

结果:

enter image description here

答案 1 :(得分:2)

您可以使用此XSLT:

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

  <xsl:key name="k" match="Form" use="@PRONME"/>

  <xsl:template match="/">
    <xsl:apply-templates select="*/Form[generate-id(.) = generate-id(key('k', @PRONME))]"/>
  </xsl:template>

  <xsl:template match="Form">
    <Total PRONME="{@PRONME}" PPRONME="{@PPRONME}" 
           TBONUSP="{sum(//Form[@PRONME = current()/@PRONME]/@TBONUSP)}"
           NACRES="{sum(//Form[@PRONME = current()/@PRONME]/@NACRES)}"/>
  </xsl:template>

</xsl:stylesheet>

输出:

<Total PRONME="Air Prospect 2" PPRONME="0" TBONUSP="3000" NACRES="6" />
<Total PRONME="Air Prospect Name" PPRONME="0" TBONUSP="1000" NACRES="2" />