>From xml-tech-bounce@xmlfr.org Wed Oct 6 17:21:13 2004
>...
>Je sèche lamentablement sur la possibilité de faire en une seule passe XSLT 1.0 la somme des produits pour un document initial du style :
><produit>
> <commande quantite="2" prix_unitaire="25.6"/>
> <commande quantite="1" prix_unitaire="24.8"/>
></produit>
Voici une solution qui fonctionne. Toutefois si quelqu'un a quelque chose
de plus simple...
<xsl:template match="produit">
<xsl:variable name="next-command" select="commande[1]"/>
<produit>
<xsl:attribute name="total">
<xsl:choose>
<xsl:when test="$next-command">
<xsl:apply-templates select="$next-command"/>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</produit>
</xsl:template>
<xsl:template match="commande">
<xsl:param name="accumulator" select="1"/>
<xsl:variable name="curent-product" select="$accumulator * @prix_unitaire"/>
<xsl:variable name="next-command" select="following-sibling::commande[1]"/>
<xsl:choose>
<xsl:when test="$next-command">
<xsl:apply-templates select="$next-command">
<xsl:with-param name="accumulator" select="$current-product"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$current-product"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
Voila. On peut aussi :
- se passer des variables qui ne sont la que pour eviter les multiples
evaluations de la meme expression ;
- supprimer le test dans le premier "template" s'il y a au moins un
produit.
Bien a vous,
J.-M. Hufflen
--
Devenez redacteur <XML>fr et contribuez au developpement du
xml francophone (http://xmlfr.org/infos/redacteurs/) !
Liste de diffusion "xml-tech@xmlfr.org" (http://xmlfr.org).
Cette liste est a votre disposition pour discuter en francais de
tout sujet technique lie a XML.
Pour resilier votre abonnement, envoyez un message contenant
la commande "unsubscribe" a xml-tech-request@xmlfr.org
(mailto:xml-tech-request@xmlfr.org?Subject=unsubscribe)
Received on Wed Oct 6 17:51:54 2004