【问题标题】:XSLT Pivot TableXSLT 数据透视表
【发布时间】:2009-10-05 21:42:45
【问题描述】:

我是使用 XSLT 的新手,我正在尝试使用 Muenchian 方法创建数据透视表(因为 IE 似乎仍然不支持 XSLT 2.0,所以我认为我对此感到困惑)。我能够获得所需的分组,但是我试图获得每个组的属性总和。要对属性求和,我可以使用聚合求和函数还是必须遍历键并将值存储到变量中?这是我目前所拥有的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>

<xsl:key name="Person" match="Record" use="@PersonID" />
<xsl:template match="/">
  <html>
  <body>
    <h2>Costs Per Person</h2>
    <table border = "1">
        <thead>
        <tr>
           <th>ID</th>
           <th>Cost</th>
        </tr>
        </thead>
        <tbody>
        <xsl:for-each select="Records/Record[generate-id() = 
         generate-id(key('Person', @PersonID)[1])]">
        <tr>
            <td>
                <xsl:value-of select="@PersonID" />
            </td>

            <td>
                <!-- Sum Of Cost -->
            </td>
        </tr>   
       </xsl:for-each>  
       </tbody>
    </table>
  </body>
  </html>
</xsl:template>

【问题讨论】:

    标签: xml xslt pivot-table


    【解决方案1】:

    容易 - 你已经在那里了。请参阅下面的完整解决方案。

    另外,我建议不要使用&lt;xsl:for-each&gt;,而是使用&lt;xsl:apply-templates&gt;。代码有点长,但可读性和代码结构有所改善,恕我直言。

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
      <xsl:output method="html" indent="yes" encoding="UTF-8"/>
    
      <xsl:key name="Person" match="Record" use="@PersonID" />
    
      <xsl:template match="/">
        <html>
          <body>
            <h2>Costs Per Person</h2>
            <table border = "1">
              <thead>
                <tr>
                   <th>ID</th>
                   <th>Cost</th>
                </tr>
              </thead>
              <tbody>
                <xsl:apply-templates select="Records/Record" />
              </tbody>
            </table>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="Records/Record">
        <xsl:variable name="thisGroup" select"key('Person', @PersonID)" />
    
        <xsl:if test="generate-id() = generate-id($thisGroup[1])">
          <tr>
            <td>
              <xsl:value-of select="@PersonID" />
            </td>
            <td>
              <!-- Sum Of Cost -->
              <xsl:value-of select="sum($thisGroup/@Cost)" />
            </td>
          </tr>   
        </xsl:if>  
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      因为您当前在循环中的上下文节点是一个 Record 元素,您需要确保您的“总和”包括具有匹配 PersonID 属性的所有记录。应该这样做:

      <xsl:value-of select="sum(../Record[@PersonID=current()/@PersonID]/@Cost)" />
      

      或者,因为您知道当前 Record 元素是第一个具有特定 PersonID 属性的元素,所以在这种情况下您也可以这样做

      <xsl:value-of select="number(@Cost) + sum(following-sibling::Record[@PersonID=current()/@PersonID]/@Cost)" />
      

      【讨论】:

      • 感谢您指出一些我不知道的语法。到目前为止,只花了几天时间使用 XSLT,所有示例都有帮助!
      【解决方案3】:

      如果您可以使用 xpath 来选择所需记录的属性,您应该能够使用 sum。

      在不知道输入 xml 的结构的情况下,我不知道这会是什么,但我猜你的情况类似于 &lt;xsl:value-of select="sum(@cost)"/&gt;

      【讨论】:

        猜你喜欢
        • 2019-04-27
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-13
        • 2015-06-25
        相关资源
        最近更新 更多