2013-02-18 3 views
0

주문 가격 합계를 추가하여 내 XML을 한 형식에서 다른 형식으로 변환해야합니다. 계산은 Sum total (itemPrice*itemQty)입니다. 내 요청 XML 내가 추가 광고를 calucluating 후 아래의 형식이 변환 필요 XSLT 변환. 곱하기 및 합계

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
     <soap:Body> 
      <ns2:fetchOrderListResponse xmlns:ns2="http://impl.lob.xyz.com/"> 
      <return> 
       <customerOrderNumber>1</customerOrderNumber> 
       <orderDetails> 
        <itemPrice>2.0</itemPrice> 
    <itemQty>1</itemQty> 
        <orderDetailsId>37516016-D71B-4790-951F-55D00B0CC159</orderDetailsId> 
       </orderDetails> 
       <orderDetails> 
        <itemPrice>5.0</itemPrice> 
        <itemQty>3</itemQty> 
        <itemUnit>0</itemUnit> 
       </orderDetails> 
       <orderId>84EC371D-40CA-455E-A0FA-7EA733E9BFD3</orderId> 
      </return> 
      <return> 
       <customerOrderNumber>1</customerOrderNumber> 
       <deliverydate>2013-02-06T00:00:00+05:30</deliverydate> 
       <orderDetails> 
        <itemPrice>7.0</itemPrice> 
        <itemQty>1</itemQty> 
        <orderDetailsId>9A5030BE-F95F-4C62-B5A2-41FF85423218</orderDetailsId> 
       </orderDetails> 
       <orderDetails> 
        <itemPrice>9.0</itemPrice> 
        <itemQty>5</itemQty> 
        <orderDetailsId>65A8B3BE-D407-43D8-8754-EA1E26AA56E4</orderDetailsId> 
       </orderDetails> 
       <orderId>0BDCB222-0117-47A9-8813-DF03A1D19E5E</orderId> 
      </return> 
      </ns2:fetchOrderListResponse> 
     </soap:Body> 
    </soap:Envelope> 

다음입니까? XSLT를 사용하는 요소. 변환 된 XML은 다음과 같아야합니다. 여기서 친절히 도와주세요. 당신은 그것을 의미 질문 XSLT2.0를 태그 한

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
     <ns2:fetchOrderListResponse xmlns:ns2="http://impl.lob.xyz.com/"> 
     <return> 
      <customerOrderNumber>1</customerOrderNumber> 
      <orderDetails> 
       <itemPrice>2.0</itemPrice> 
       <itemQty>1</itemQty> 
       <orderDetailsId>37516016-D71B-4790-951F-55D00B0CC159</orderDetailsId> 
      </orderDetails> 
      <orderDetails> 
       <itemPrice>5.0</itemPrice> 
       <itemQty>3</itemQty> 
      </orderDetails> 
      <orderId>84EC371D-40CA-455E-A0FA-7EA733E9BFD3</orderId> 
      **<ordertotal>17.0</ordertotal>** 
     </return> 
     <return> 
      <customerOrderNumber>1</customerOrderNumber> 
      <deliverydate>2013-02-06T00:00:00+05:30</deliverydate> 
      <orderDetails> 
       <itemPrice>7.0</itemPrice> 
       <itemQty>1</itemQty> 
       <orderDetailsId>9A5030BE-F95F-4C62-B5A2-41FF85423218</orderDetailsId> 
      </orderDetails> 
      <orderDetails> 
       <itemPrice>9.0</itemPrice> 
       <itemQty>5</itemQty> 
       <orderDetailsId>65A8B3BE-D407-43D8-8754-EA1E26AA56E4</orderDetailsId> 
      </orderDetails> 
      <orderId>0BDCB222-0117-47A9-8813-DF03A1D19E5E</orderId> 
      **<ordertotal>52.0</ordertotal>** 
     </return> 
     </ns2:fetchOrderListResponse> 
    </soap:Body> 
</soap:Envelope> 
+0

가 어디 귀하의 XSLT? – Torious

+0

초보자는 다음을 시도하십시오. http://www.dpawson.co.uk/xsl/sect2/N1930.html#d3155e175 – rene

답변

1

XSLT 2.0 솔루션은 다음과 같습니다

<xsl:stylesheet version="2.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes" /> 

    <!-- Identity template : elements and attributes are copied by default --> 
    <xsl:template match="*|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="*|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <!-- When matching return we add the order total as its last child --> 
    <xsl:template match="return"> 
     <xsl:copy> 
      <xsl:copy-of select="@*|*" /> 
      <ordertotal> 
       <xsl:value-of select="sum(orderDetails/(itemPrice*itemQty))" /> 
      </ordertotal> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

당신은 당신이 원하는 것을 달성하기 위해 재귀를 사용해야합니다 (확장 기능 없음) XSLT 1.0을 사용하는 경우 :

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="*|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="*|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="return"> 
     <xsl:copy> 
      <xsl:copy-of select="@*|*" /> 
      <ordertotal><xsl:call-template name="calculate-total" /></ordertotal> 
     </xsl:copy> 
    </xsl:template> 

    <!-- Recursive template --> 
    <xsl:template name="calculate-total"> 
     <!-- Select by default the set of orderDetails from the current context --> 
     <xsl:param name="orderDetails" 
        select="orderDetails" /> 
     <!-- Param which is going to keep track of the result step by step --> 
     <xsl:param name="total" 
        select="'0'" /> 

     <xsl:choose> 
      <!-- If we have remaining order details, recurse --> 
      <xsl:when test="$orderDetails"> 
       <xsl:call-template name="calculate-total"> 
        <!-- Remove the current element for the next step --> 
        <xsl:with-param name="orderDetails" 
            select="$orderDetails[position() > 1]" /> 
        <!-- Do the partial operation for the current element, and continue to the next step --> 
        <xsl:with-param name="total" 
            select="$total + ($orderDetails[1]/itemPrice * $orderDetails[1]/itemQty)" /> 
       </xsl:call-template>   
      </xsl:when> 
      <!-- Output the result --> 
      <xsl:otherwise> 
       <xsl:value-of select="$total" /> 
      </xsl:otherwise> 
     </xsl:choose> 

    </xsl:template> 

</xsl:stylesheet> 
0

는 합산을 할 직선적이다. 당신이 반환 요소에 배치되어 가정, 당신은 단순히 ordertotal에게 단순히 XSLT 정체성 변환을 이용하여 복사 할 것

<ordertotal> 
    <xsl:value-of select="sum(orderDetails/(itemPrice * itemQty))"/> 
</ordertotal> 

다른 모든 요소를 ​​얻기 위해이 작업을 수행 할 것입니다.

여기서 전체 XSLT

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

    <xsl:template match="return"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <ordertotal> 
      <xsl:value-of select="sum(orderDetails/(itemPrice * itemQty))"/> 
     </ordertotal> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

이 제 복귀 요소하면 (17)의 순서로 합계를 제공해야하며, 52 초이다.