xsl,满足特定条件的节点总和

时间:2016-03-25 23:22:12

标签: xslt sum conditional-statements partial

我想知道如何在满足条件时获得节点总和,具有xml:

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

我想列出哪些产品有客户购买(prod / venta / @ client),以及产品的价格总和(“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>

我希望获得如下输出:

client1 prod1 prod2 sum_of_price_prod1 + prod2

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

Ex:Maria Baby Cook Texans 744.35

抱歉英语不好。

1 个答案:

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