2016-04-23 5 views
0

私のXMLは、*xsltを使用して、異なる複雑な要素の2つの異なるノードの値を積で合計する方法は?

<root> <source> <item> <id>111</id> <qty>2</qty> </item> <item> <id>222</id> <qty>1</qty> </item> <item> <id>333</id> <qty>4</qty> </item> </source> <target> <record> <id>111</id> <price>1000</price> </record> <record> <id>333</id> <price>500</price> </record> </target> </root>

は、今私はそれが私が製品

ソース/アイテム/数量に必要な一致した場合は、ソース/アイテムとターゲット/レコードのid要素を一致させる必要がありますターゲット/記録/価格

製品が一致するすべてのケースのために行われると私はすべての製品の価値を合計しなければならないと

として結果を取得する必要があります

4000すなわち(一致するすべての要素の和(数量*価格)。)

これは親切に、この上で私を助ける達成するための方法、XSLT 2.0では、事前

答えて

0

私はあなたの条件に合わせてDimitre Novatchevからこのanswerを変更しました。コードは次のとおりです:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes" omit-xml-declaration="yes"/> 

    <xsl:key name="target" match="record" use="id" /> 

    <xsl:template match="/"> 
     <xsl:call-template name="sumProducts"> 
      <!-- get only sources with respective equivalent targets --> 
      <xsl:with-param name="pList" select="*/*/item[key('target', id)]"/> 
      <!-- a list of target records --> 
      <xsl:with-param name="pList2" select="*/*/record"/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="sumProducts"> 
     <xsl:param name="pList"/> 
     <xsl:param name="pList2"/> 
     <xsl:param name="pAccum" select="0"/> 

     <xsl:choose> 
      <xsl:when test="$pList"> 
       <xsl:variable name="vHead" select="$pList[1]"/> 
       <xsl:variable name="vHead2" select="$pList2"/> 

       <xsl:call-template name="sumProducts"> 
        <xsl:with-param name="pList" select="$pList[position() > 1]"/> 
        <xsl:with-param name="pList2" select="$pList2"/> 
        <xsl:with-param name="pAccum" 
         select="$pAccum + $vHead/qty * $vHead2[id = $vHead/id]/price"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$pAccum"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 
+0

&Dimitreご回答いただきありがとうございました – Satiz

0

のおかげで、あなたが行うことができます:

<xsl:key name="target" match="record" use="id" /> 

<xsl:template match="/root"> 
    <result> 
     <xsl:value-of select="sum(source/item[key('target', id)]/(qty * key('target', id)/price))"/> 
    </result> 
</xsl:template> 
+0

あなたの答えはありがたいです、私たちはxslt 1.0を使用しています。 – Satiz

+0

なぜあなたはXSLT 2.0としてあなたの質問にタグを付けましたか? –

+0

私は2.0を知っている人もこの問題を解決するのに役立つと思いました。 – Satiz

関連する問題