2017-09-27 47 views
0
SO

嗨,总和未链接

新建XSLT和已调试该代码了一会儿

当前变量总是返回0

我需要通过每个(行)找到所有(X)与(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萨姆(当前):

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

怎么样'V'和'W'与该位置相关的是'Row'的第一个'V'子属于第一个'W'子吗? –

+0

@MartinHonnen位置是,V [x] W [x],真的不知道为什么它不仅仅是一个元素,而是我必须使用它作为 –

回答

2

我会解决它作为一个分组的问题,首先确定独特的说明,然后由描述找到秒和最终总结在相同位置的元素:

<?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> 
+0

我同意马丁。分组似乎是对所述问题的合理解决方案。 – FarligOpptreden