2016-03-25 5 views
0

Ich mag würde wissen, wie nur Summe von Knoten zu erhalten, wenn sie eine Bedingung erfüllen, mit der xml:xsl, Summe von Knoten, die eine bestimmte Bedingung erfüllt

<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> 

Ich mag eine Liste machen, von denen Produkte gekauft hat ein Client (prod/Venta/@ Client) und die Summe der Preise ("preu") der Produkte:

ein xsl wie mit:

<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> 

möchte ich bekommen ein Ausgabe wie:

client1 prod1 prod2 sum_of_price_prod1 + prod2

client2 Prod4 prod6 prod 7 sum_of_price_of (Prod4 + prod6 + prod7)

Ex: Maria Baby-Koch Texans 744,35

Sorry für die schlechte Englisch.

+0

Können Sie Ihre Frage bearbeiten zu sehen erwarten, dass die Ausgabe, die Sie zeigen? Vielen Dank! –

Antwort

1

Sie eine Variable definieren können halten alle prod Elemente aktuellen ...

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

Dann können Sie die prod Elemente Liste auswählen, wie so ...

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

Und die bekommen Summe Sie können dies tun ...

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

Versuchen Sie diese 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> 

Alternativ können Sie eine xsl:key verwenden, um die prod Elemente

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

dieses XSLT Versuchen zu

<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>