ProgrammingNewbie
ProgrammingNewbie

Reputation: 45

How do I fix my XML code?

ok, I have this XSLT code and it works perfectly, there's just one problem inside the for condition.

<xsl:template match="/polinomio">
    <HTML>
        <HEAD>
        </HEAD>
        <BODY>
        </BODY>
    </HTML>
    <derivada>
        <xsl:for-each select="termino">
            <xsl:copy>
                <coeficiente>
                    <parentesis>(</parentesis>
                    <xsl:value-of select="coeficiente * grado" />
                </coeficiente>
                <multi>*</multi>
                <xsl:copy-of select="variable"/>  
                <grado>

                    <grado>^</grado>
                    <xsl:value-of select="grado - 1" />
                </grado>
            </xsl:copy>
        <parentesis>)</parentesis>
        </xsl:for-each>
    </derivada>
</xsl:template>

</xsl:stylesheet>

this is the result I get: (6 * x ^1 ) (8 * x ^3 ) but what I need is to have a plus sign between the two parentheses: (6 * x ^1 )+(8 * x ^3 ) any idea how I should do it?

Upvotes: 0

Views: 80

Answers (1)

cis
cis

Reputation: 1377

If I get your problem right, your for-each-loop outputs the parenthesis (....) and you would like to have a plus sign between each parentheses. So, I would add the following code between <parentesis>)</parentesis> and </xsl:for-each>:

<xsl:if test="position() lt last()">+</xsl:if>

This puts a + to the output, except for the last run of the loop.

Upvotes: 2

Related Questions