2017-09-27 2 views
0

Salut SO,Somme des noeuds XSLT qui ne sont pas liés

Nouveau sur XSLT et ont été débogage ce code pendant un certain temps

La variable actuelle retourne toujours 0

J'ai besoin pour trouver la somme de tous (X) avec la même valeur (D) à travers chaque (rangée)

V et W sont liés, ne savez pas comment « connecter » les

Exemple:

Row (AAA123)[SomeDesc1] = 1.00 + 

Row (BBB456)[SomeDesc1] = 3.00 

SumOfSomeDesc1 = 4.00 

XSLT 1.0 ne

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> 

Somme XSLT (actuelle):

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

Comment sont 'V' et' W' liés, par la position, qui est le premier 'V' enfant d'un' Row' appartient au premier 'W' enfant? –

+0

@MartinHonnen Position oui, V [x] W [x], vraiment pas sûr pourquoi ce n'était pas juste un élément mais je dois l'utiliser tel quel –

Répondre

2

Je l'aborder comme un problème de regroupement, d'abord d'identifier les descriptions uniques, puis de trouver s par la description et, enfin, résumant les éléments dans la même position:

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

Résultat est

<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

Je suis d'accord avec Martin. Le regroupement semble être la solution logique au problème énoncé. – FarligOpptreden