2016-03-25 62 views
0

我想知道如何獲得節點的總和,只有當他們滿足條件的節點的總和,具有XML:XSL,滿足一定條件

<segma> 
    <conservacio> 
     <cons estat="A">Excel·lent conservació</cons> 
     <cons estat="B">Bona conservació</cons> 
     <cons estat="C">Regular conservació</cons> 
     <cons estat="D">Mala conservació</cons> 
    </conservacio> 

    <categories> 
     <categoria cat="1">Mobiliari</categoria> 
     <categoria cat="2">Alimentació</categoria> 
     <categoria cat="3">Roba</categoria> 
    </categories> 

    <clients> 
     <registre dni="111">Marti</registre> 
     <registre dni="222">Jana</registre> 
     <registre dni="333">Edu</registre> 
     <registre dni="444">Santi</registre> 
     <registre dni="555">Mia</registre> 
     <registre dni="666">Pau M</registre>   
    </clients> 

    <productes> 
     <prod id="1" cons="A" cat="1"> 
      <nom>Cuna</nom> 
      <preu_nou>70</preu_nou> 
      <preu>30</preu> 
      <ofertes> 
       <oferta client="111" /> 
       <oferta client="333" /> 
      </ofertes> 
      <venta client="111" /> 
     </prod> 

     <prod id="2" cons="B" cat="2"> 
      <nom>Baby cook</nom> 
      <preu_nou>120</preu_nou> 
      <preu>60</preu> 
      <ofertes> 
       <oferta client="111" /> 
       <oferta client="333" /> 
       <oferta client="444" /> 
       <oferta client="555" /> 

       </ofertes> 
      <venta client="555" /> 
     </prod>   

     <prod id="3" cons="A" cat="1"> 
      <nom>Mama pata</nom> 
      <preu_nou>70</preu_nou> 
      <preu>5</preu> 
      <ofertes> 
       <oferta client="444" /> 
       <oferta client="555" /> 
      </ofertes> 
      <venta client="444" /> 
     </prod>  

     <prod id="4" cons="B" cat="3"> 
      <nom>Conjunt xandall</nom> 
      <preu_nou>40</preu_nou> 
      <preu>15</preu> 
      <ofertes> 
       <oferta client="222" /> 
       <oferta client="555" /> 
      </ofertes> 
      <venta client="222" /> 
     </prod> 

     <prod id="5" cons="C" cat="3"> 
      <nom>Pack 3 texans</nom> 
      <preu_nou>70</preu_nou> 
      <preu>25</preu> 
      <ofertes> 
       <oferta client="333" /> 
       <oferta client="444" /> 
       <oferta client="555" /> 
      </ofertes> 
      <venta client="333" /> 
     </prod> 

     <prod id="6" cons="A" cat="2"> 
      <nom>Saca leches</nom> 
      <preu_nou>130</preu_nou> 
      <preu>70</preu> 
      <ofertes> 
       <oferta client="111" /> 
       <oferta client="222" /> 
       <oferta client="555" /> 
      </ofertes> 
      <venta client="111" /> 
     </prod>  

     <prod id="7" cons="C" cat="2"> 
      <nom>Llet continuació</nom> 
      <preu_nou>11</preu_nou> 
      <preu>3</preu> 
      <ofertes> 
      </ofertes> 
     </prod>    

    </productes> 
</segma> 

我想打哪個列表產品已經在客戶端購買(產品/文塔/ @客戶端),以及價格的乘積之和(「preu」):擁有一個像一個xsl

<h2>Ex 4</h2> 
     <table border="1"> 
    <xsl:for-each select="//registre"> 
    <xsl:variable name="dni" select="@dni"/> 

    <tr> 
    <td> <xsl:value-of select="."/></td> 

    <xsl:for-each select="//prod"> 

    <xsl:if test="venta/@client=$dni"> 
    <td><xsl:value-of select="nom"/></td> 

    </xsl:if> 
    </xsl:for-each> 

我想獲得一個輸出如:

客戶端1 PROD1 Prod2的sum_of_price_prod1 + Prod2的

客戶端2 prod4 prod6督促7 sum_of_price_of(prod4 + prod6 + prod7)

例如:瑪麗亞·庫克寶貝德州744.35

對不起,我英文不好。

+0

您可以編輯您的問題,以顯示輸出,你期待什麼呢?謝謝! –

回答

1

可以定義一個變量來保存所有prod元件的電流...

<xsl:variable name="prod" select="//prod[venta/@client=$dni]" /> 

則可以列出選擇prod元素,像這樣......

<xsl:for-each select="$prod"> 

而得到總和你可以做到這一點...

<xsl:value-of select="sum($prod/preu)" /> 

試試這個XSLT

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

<xsl:template match="/"> 
    <h2>Ex 4</h2> 
     <table border="1"> 
      <xsl:for-each select="//registre"> 
       <xsl:variable name="dni" select="@dni"/> 
       <xsl:variable name="prod" select="//prod[venta/@client=$dni]" /> 
       <tr> 
        <td><xsl:value-of select="."/></td> 
        <td> 
        <xsl:for-each select="$prod"> 
          <xsl:value-of select="nom"/><br /> 
        </xsl:for-each> 
        </td> 
        <td> 
         <xsl:value-of select="sum($prod/preu)" /> 
        </td> 
       </tr> 
      </xsl:for-each> 
     </table> 
    </xsl:template> 
</xsl:stylesheet> 

或者,你可以使用一個xsl:key來查找prod元素

<xsl:key name="prod" match="prod" use="venta/@client" /> 

試試這個XSLT太

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

<xsl:key name="prod" match="prod" use="venta/@client" /> 

<xsl:template match="/"> 
    <h2>Ex 4</h2> 
     <table border="1"> 
      <xsl:for-each select="//registre"> 
       <tr> 
        <td><xsl:value-of select="."/></td> 
        <td> 
        <xsl:for-each select="key('prod', @dni)"> 
          <xsl:value-of select="nom"/><br /> 
        </xsl:for-each> 
        </td> 
        <td> 
         <xsl:value-of select="sum(key('prod', @dni)/preu)" /> 
        </td> 
       </tr> 
      </xsl:for-each> 
     </table> 
    </xsl:template> 
</xsl:stylesheet>