XSLT 数据透视表

发布于 2024-08-06 23:53:06 字数 1199 浏览 2 评论 0原文

我是使用 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>

I am new to working with XSLT and am trying to create a pivot table using the Muenchian Method (since it appear that IE still doesn't support XSLT 2.0 I think I'm stuck with this). I am able to get the desired grouping however I am trying to get the sum of an attribute for each group. To do the sum of the attribute can I use the aggregate sum function or do I have to loop through the keys and store the values into a variable? This is what I have so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蔚蓝源自深海 2024-08-13 23:53:06

很简单——你已经在那里了。请参阅下面的完整解决方案。

另一方面,我建议不要使用 ,而是使用 。代码变得有点长,但可读性和代码结构得到了改善,恕我直言。

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

Easy - you where already there. See full solution below.

On a different note, I advise against the use of <xsl:for-each> in favor of <xsl:apply-templates>. The code gets a bit longer, but readability and code structure improves, IMHO.

<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>
淡看悲欢离合 2024-08-13 23:53:06

由于循环内当前的上下文节点是 Record 元素,因此您需要确保“sum”包含具有匹配 PersonID 属性的所有 Record。应该这样做:

<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)" />

Because your current context node inside the loop is a Record element, you will need to ensure your 'sum' includes all Records with a matching PersonID attribute. Something like this should do:

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

Or, because you know the current Record element is the first with a particular PersonID attribute, you could also do this in this case

<xsl:value-of select="number(@Cost) + sum(following-sibling::Record[@PersonID=current()/@PersonID]/@Cost)" />
请别遗忘我 2024-08-13 23:53:06

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

在不知道输入 xml 结构的情况下,我不知道这会是什么,但我猜在您的情况下类似于

You should be able to use sum if you can use an xpath to select the attribute for the desired record(s).

Without knowing the structure of your input xml i don't know what this would be but I guess in your case something like <xsl:value-of select="sum(@cost)"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文