2013-02-27 5 views
1

저는 XSLT의 초보자이며 결과를 얻는 데 어려움을 겪고 있으며 누구나 나를 도울 수 있기를 바랍니다.값을 그룹화하고 합산하기위한 XSLT

I가 다음과 같은 XML :

<funds> 
    <bags> 
     <bag name="BAG_USD_MAIN" value="10.0" type="USD"> 
      <pockets> 
       <pocket name="bank" value="7.5"> 
       <pocket name="award" value="2.5"> 
      </pockets> 
     </bag> 
     <bag name="BAG_USD_SAVINGS" value="290.75" type="USD"> 
      <pockets> 
       <pocket name="bank" value="290.75"> 
      </pockets> 
     </bag> 
     <bag name="BAG_EUR_EXTRA" value="890.0" type="EUR"> 
      <pockets> 
       <pocket name="bank" value="753.0"> 
       <pocket name="bank_eng" value="137.0"> 
      </pockets> 
     </bag> 
    </bags> 
</funds> 

그리고 내가 이런 식으로 변환 할 수 있도록하고 싶습니다 :

<result> 
    <total type="USD">375.0</total> 
    <total type="EUR">890.0</total> 
</result> 

을가 XSLT와 함께 할 수 있습니까?

감사합니다, 감사 TS

+0

XSLT 1.0 또는 2.0? –

+0

XSLT 2.0이 될 것입니다. –

답변

0

예. 나는 XSLT 프로세서가 편리 가지고 있지만, 다음과 같은 뭔가를하려고하지 않습니다

<result> 
    <xsl:for-each select="distinct-values(//bag/@type)"> 
    <total type="."><xsl:value-of select="sum(//bag[@type = .])"/></total> 
    </xsl:for-each> 
</result> 

는 더 많은 양의 데이터를 들어, 대신 <xsl:for-each-group>을 사용하는 것이 좋습니다.

+3

데이터가 단순하지 않은 경우에만 맛의 문제가 아닙니다. 각 그룹 용 솔루션은 훨씬 빠를 것입니다. –

+0

충분하다, +1; 그에 따라 내 게시물을 편집합니다. –

2

당신이 < XSL을 사용할 수 XSLT 2.0를 사용하는 이유는 그룹의 요소를 합계 합()을 사용해서 @type 그룹화 요소들> 요소 - 각각의 그룹과.

다음 스타일 시트

당신이 뭘 하려는지 해결 :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/> 


    <!-- Match the bags element --> 
    <xsl:template match="bags"> 
     <result> 
      <!-- Group each bag element by its type --> 
      <xsl:for-each-group select="bag" group-by="@type"> 
       <!-- Use the current key to display the type attribute --> 
       <total type="{current-grouping-key()}"> 
        <!-- Sum all the elements from the current group --> 
        <xsl:value-of select="sum(current-group()/@value)" /> 
       </total> 
      </xsl:for-each-group> 
     </result> 
    </xsl:template> 

</xsl:stylesheet> 

그냥 완전성에 대한 XSLT 1.0 솔루션 Muenchian 그룹화를 기반으로 될 것이다.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/> 

    <!-- Index all bag elements withing bags element using their 
     type attribute by using a key --> 
    <xsl:key name="currency-key" 
      match="/funds/bags/bag" 
      use="@type" /> 

    <!-- Match bags element --> 
    <xsl:template match="bags"> 
     <result> 
      <!-- Match the first bag element for a specific group --> 
      <xsl:apply-templates select="bag[generate-id() = generate-id(key('currency-key', @type)[1])]" /> 
     </result> 
    </xsl:template> 

    <xsl:template match="bag"> 
     <total type="{@type}"> 
      <!-- Sum all the elements from the @type group --> 
      <xsl:value-of select="sum(key('currency-key', @type)/@value)" /> 
     </total> 
    </xsl:template> 

</xsl:stylesheet> 
+0

고마워요 파블로. 이것은 속임수를했다! !! 고마워요 !!!! –

+0

기꺼이 도와 드리겠습니다;) –

관련 문제