2017-01-09 3 views
0

XSLT를 처음 사용 했으므로 각 주문의 ID를 기반으로 항목의 총 가격 (수량 * 단가)을 합산하고 XSLT 1.0을 사용하여 각 항목의 그룹 끝에 인쇄해야합니다. 여기에 내 예 XML이다XSLT 그룹화 및 합계

<Orders> 
<Order> 
    <Reference>234</Reference> 
    <Item> 
     <ID>10</ID> 
     <Quantity>1</Quantity> 
     <UnitPrice>2</UnitPrice> 
    </Item> 
    <Item> 
     <ID>10</ID> 
     <Quantity>2</Quantity> 
     <UnitPrice>3</UnitPrice> 
    </Item> 
    <Item> 
     <ID>10</ID> 
     <Quantity>2</Quantity> 
     <UnitPrice>2</UnitPrice> 
    </Item> 
    <Item> 
     <ID>20</ID> 
     <Quantity>2</Quantity> 
     <UnitPrice>4</UnitPrice> 
    </Item> 
</Order> 
<Order> 
    <Reference>456</Reference> 
    <Item> 
     <ID>10</ID> 
     <Quantity>2</Quantity> 
     <UnitPrice>2</UnitPrice> 
    </Item> 
    <Item> 
     <ID>20</ID> 
     <Quantity>2</Quantity> 
     <UnitPrice>2</UnitPrice> 
    </Item> 
</Order> 
</Orders> 

<SAPOrders> 
<Order> 
    <Reference>234</Reference> 
    <Item> 
     <Quantity>1</Quantity> 
     <UnitPrice>2</UnitPrice> 
    </Item> 
    <Item> 
     <Quantity>2</Quantity> 
     <UnitPrice>3</UnitPrice> 
    </Item> 
    <Item> 
     <Quantity>2</Quantity> 
     <UnitPrice>2</UnitPrice> 
     <Total>12</Notes> 
    </Item> 
    <Item> 
     <Quantity>2</Quantity> 
     <UnitPrice>4</UnitPrice> 
     <Total>8</Notes> 
    </Item> 
</Order> 
<Order> 
    <Reference>456</Reference> 
    <Item> 
     <Quantity>2</Quantity> 
     <UnitPrice>2</UnitPrice> 
     <Total>4</Notes> 
    </Item> 
    <Item> 
     <Quantity>2</Quantity> 
     <UnitPrice>2</UnitPrice> 
     <Total>4</Total> 
    </Item> 
</Order> 
</SAPOrders> 
+0

는 참조 http://stackoverflow.com/questions/37205574/sum-of-similar-elements-in-xslt 거의 중복되는 –

+1

@MichaelKay XSLT 2.0 솔루션을 가리키고 있습니다. –

+1

@tojira 출력에 항목 ID가없는 이유는 무엇입니까? –

답변

0

여기서 두 가지 문제점이있다 아래 원하는 출력 XML은 : (1) 군 아이템 ID에 의해 순서 의해 항목 및 (2) 각 그룹에 대한 부분합을 계산하십시오.

첫 번째 문제는 비교적 쉽고 Muenchian grouping 방법을 사용하여 해결할 수 있습니다. 두 번째 문제는 XSLT 1.0이 계산 결과 합계를 허용하지 않기 때문에 더 어렵습니다.

다음 스타일 시트는 각 품목의 확장 가격을 미리 계산하고 그 결과를 변수에 저장함으로써 시작됩니다. 그런 다음 변수를 조작하여 항목을 그룹화하고 각 그룹을 부분 합산합니다.

XSLT 1.0

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

<xsl:key name="item" match="Item" use="@key" /> 

<xsl:template match="/Orders"> 
    <!-- first pass --> 
    <xsl:variable name="first-pass"> 
     <xsl:for-each select="Order"> 
      <Order> 
       <xsl:copy-of select="Reference"/> 
       <xsl:for-each select="Item"> 
        <Item key="{concat(../Reference, '|', ID)}" extPrice="{Quantity * UnitPrice}"> 
         <xsl:copy-of select="*"/> 
        </Item> 
       </xsl:for-each> 
      </Order> 
     </xsl:for-each> 
    </xsl:variable> 
    <!-- output --> 
    <SAPOrders> 
     <xsl:for-each select="exsl:node-set($first-pass)/Order"> 
      <xsl:copy> 
       <xsl:copy-of select="Reference"/> 
       <!-- for each unique item in this order --> 
       <xsl:for-each select="Item[count(. | key('item', @key)[1]) = 1]"> 
        <!-- list the items in this group --> 
        <xsl:for-each select="key('item', @key)"> 
         <Item> 
          <xsl:copy-of select="Quantity | UnitPrice"/> 
          <!-- add the subtotal of this group --> 
          <xsl:if test="position()=last()"> 
           <Total> 
            <xsl:value-of select="sum(key('item', @key)/@extPrice)" /> 
           </Total> 
          </xsl:if> 
         </Item> 
        </xsl:for-each> 
       </xsl:for-each> 
      </xsl:copy> 
     </xsl:for-each> 
    </SAPOrders> 
</xsl:template> 

</xsl:stylesheet> 

데모 : http://xsltransform.net/jz1PuPz

+0

해답을 가져 주셔서 감사합니다! 잘 작동한다! – tojira