html - XSLT 总计和小计

标签 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

最佳答案

要以 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

关于html - XSLT 总计和小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6799400/

相关文章:

javascript - 使用按钮显示一些文本 - JavaScript

html - ul 内的 2 列 li,不会填充 100% 的 ul 宽度

java - 膨胀类 fragment GoogleMaps 时出错

xslt 与 Saxon 合并函数

java - 使用 xsl 将 XML 传输到 CSV 期间的空行

javascript - 当我只对一个 div 使用 jQuery scrollLeft 时,整个页面在顶部滚动

javascript - 滚动到动画

android - 在Android中使用SVG作为 ImageView 的资源

Android XML 解析器不工作

xml - 选择多变量 XSL 中的第一个节点