Consider the following XML structure:
<a>
<t>abc</t>
</a>
<a type="start"></a>
<b>
<t>ignore</t>
</b>
<a></a>
<a>
<t>1</t>
</a>
<a>
<t>2</t>
</a>
<a>
<t>3</t>
</a>
<b>
<t>ignore</t>
</b>
<a>
<t>4</t>
</a>
<a type="end"></a>
<a>
<t>def</t>
</a>
I need to get the sum of the contents of all tags abetween tags awith attribute value startand end.
I tried it with the following XSL:
<xsl:template match="a">
<xsl:choose>
<xsl:when test="@type='start'">
<merged>
<xsl:call-template name="getMergedText">
<xsl:with-param name="text" select="''"/>
<xsl:call-template>
</merged>
</xsl:when>
<xsl:otherwise>
<single>
<xsl:value-of select="t"/>
</single>
</xsl:otherwise>
<xsl:choose>
</xsl:template>
<xsl:template name="getMergedText">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="following-sibling::a[1]/@type='end'">
<xsl:value-of select="$text"/>
</xsl:when>
<xsl:when test="following-sibling::a[1]/t">
<xsl:variable name="text.update">
<xsl:value-of select="$text"/>
<xsl:value-of select="following-sibling::a[1]/t"/>
</xsl:variable>
<xsl:for-each select="following-sibling::a[1]">
<xsl:call-template name="getMergedText">
<xsl:with-param name="text" select="$text.update"/>
<xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="following-sibling::a[1]">
<xsl:call-template name="getMergedText">
<xsl:with-param name="text" select="$text"/>
<xsl:call-template>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Required Conclusion:
<single>abc</single>
<merged>1234</merged>
<single>def</single>
The output I get is:
<single>abc</single>
<merged>1234</merged>
<single>1</single>
<single>2</single>
<single>3</single>
<single>4</single>
<single>def</single>
How can I avoid reprocessing nodes aalready processed by the template getMergedText?
Thnx in advance!
Note. I am working with XSLT 1.0. In XML, there can be several instances of pairs of starting end nodes with any number of nodes before, after, and between pairs.
source
share