2017-01-23 2 views
0

Ich möchte Summierung der Bevölkerung pro Zeitstempel & Reasoncode tun. Es funktionierte nicht, mit dem folgenden Beispielcode. Können Sie mir helfen pls es arbeitenWie summe basierend auf 2 Elementen mit XSLT

Meine XML bei der Herstellung:

ich folgende XML als Eingabedatei

<?xml version="1.0" encoding="UTF-8"?> 
<Earnings> 
    <Collection> 
     <Timestamp>20170101</Timestamp> 
     <Points>100</Points> 
     <ReasonCode>AN</ReasonCode> 
    </Collection> 
    <Collection> 
     <Timestamp>20170102</Timestamp> 
     <Points>100</Points> 
     <ReasonCode>AN</ReasonCode> 
    </Collection> 
    <Collection> 
     <Timestamp>20170101</Timestamp> 
     <Points>10000</Points> 
     <ReasonCode>BP</ReasonCode> 
    </Collection> 
    <Collection> 
     <Timestamp>20170101</Timestamp> 
     <Points>10000</Points> 
     <ReasonCode>BP</ReasonCode> 
    </Collection> 
    <Collection> 
     <Timestamp>20170102</Timestamp> 
     <Points>100</Points> 
     <ReasonCode>PPTS</ReasonCode> 
    </Collection> 
</Earnings> 

Mein XSLT

ich verwendet habe, die folgende XSLT verwendet haben, verwendet um mein XML in das angegebene Format zu konvertieren

<?xml version="1.0" encoding="UTF-16"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:key name="Earnings" match="Earnings/Collection" use="concat(Timestamp, '+',ReasonCode)"/> 
    <xsl:template match="Earnings"> 
     <Earnings> 
      <xsl:for-each select="Collection[generate-id() = generate-id(key('Earnings', concat(Timestamp, '+',ReasonCode))[1])]"> 
       <xsl:sort select="concat(Timestamp, '+',ReasonCode)"/> 
       <xsl:variable name="current-group" select="/Earnings/Collection[ReasonCode = current()/ReasonCode]"/> 
       <Collection> 
        <Timestamp> 
         <xsl:value-of select="Timestamp"/> 
        </Timestamp> 
        <Points> 
         <xsl:value-of select="sum($current-group/Points)"/> 
        </Points> 
        <ReasonCode> 
         <xsl:value-of select="ReasonCode"/> 
        </ReasonCode> 
       </Collection> 
      </xsl:for-each> 
     </Earnings> 
    </xsl:template> 
</xsl:stylesheet> 

Erwarteter Ausgang

<Collection> 
    <Timestamp>20170101</Timestamp> 
    <Points>100</Points> 
    <ReasonCode>AN</ReasonCode> 
</Collection> 
<Collection> 
    <Timestamp>20170101</Timestamp> 
    <Points>2000</Points> 
    <ReasonCode>BP</ReasonCode> 
</Collection> 
<Collection> 
    <Timestamp>20170102</Timestamp> 
    <Points>100</Points> 
    <ReasonCode>AN</ReasonCode> 
</Collection> 
<Collection> 
    <Timestamp>20170102</Timestamp> 
    <Points>100</Points> 
    <ReasonCode>PPTS</ReasonCode> 
</Collection> 

Danke,
Mohan

Antwort

0

Wechsel:

<xsl:variable name="current-group" select="/Earnings/Collection[ReasonCode = current()/ReasonCode]"/> 

zu:

<xsl:variable name="current-group" select="key('Earnings', concat(Timestamp, '+',ReasonCode))"/> 

Was haben Sie jetzt fasst alle Collection s mit dem gleichen ReasonCode wie die aktuelle Collection, in Bezug auf s ihrer Timestamp.

+0

Danke Michael. Es funktioniert gut. –

Verwandte Themen