未链接的 XSLT 节点的总和



嗨,

XSLT的新手,并且一直在调试此代码一段时间

当前变量始终返回0

我需要找到所有(x)的总和(x)的总和(d)通过每个(d)

v和w是相关的,不确定如何"连接"它们

示例:

Row (AAA123)[SomeDesc1] = 1.00 + 
Row (BBB456)[SomeDesc1] = 3.00 
SumOfSomeDesc1 = 4.00

XSLT 1.0

XML:

<Root>
  <Row>
    <ID>AAA123</ID>
    <V>
      <X>1.00</X>
    </V>
    <V>
      <X>2.00</X>
    </V>
    <MultipleFieldsInBetween />
    <W>
      <D>SomeDesc1</D>
    </W>
    <W>
      <D>SomeDesc2</D>
    </W>
  </Row>
  <Row>
    <ID>BBB456</ID>
    <V>
      <X>3.00</X>
    </V>
    <V>
      <X>4.00</X>
    </V>
    <MultipleFieldsInBetween />
    <W>
      <D>SomeDesc1</D>
    </W>
    <W>
      <D>SomeDesc2</D>
    </W>
  </Row>
</Root>

xslt sum(当前):

<xsl:variable name="SumOfX" select="sum(//Row[ID/text()=$ID]/V[D/text()
=$Description])" />

我将其作为分组问题解决,首先识别独特的描述,然后通过描述找到Row s,最后将元素总结在相同位置:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:key name="desc-group" match="Row/W/D" use="."/>
    <xsl:variable name="desc-groups" select="//Row/W/D[generate-id() = generate-id(key('desc-group', .)[1])]"/>
    <xsl:key name="row-group" match="Row" use="W/D"/>
    <xsl:template match="/Root">
        <html>
            <body>
                <table>
                    <thead>
                        <tr>
                           <th>Description</th>
                           <th>Sum</th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:apply-templates select="$desc-groups"/>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Row/W/D">
        <tr>
            <td>
                <xsl:value-of select="."/>
            </td>
            <td>
                <xsl:variable name="pos" select="count(.. | ../preceding-sibling::W)"/>
                <xsl:value-of select="sum(key('row-group', .)/V[position() = $pos]/X)"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

结果是

<html>
   <body>
      <table>
         <thead>
            <tr>
               <th>Description</th>
               <th>Sum</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>SomeDesc1</td>
               <td>4</td>
            </tr>
            <tr>
               <td>SomeDesc2</td>
               <td>6</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

相关内容

  • 没有找到相关文章

最新更新