Re: [xsl] impossible to implement this algorithm in xslt?

Subject: Re: [xsl] impossible to implement this algorithm in xslt?
From: Christoph Naber <dio2000@xxxxxxx>
Date: Fri, 25 Jul 2008 08:41:33 +0200
I'm not 100% sure about this solution, but I think it should work. You propably want to do the work by using the preceding-axis.

<xsl:template match="/" >
	<xsl:apply-templates select="*" />
</xsl:template>

<xsl:template match="sect" >
	<xsl:copy>
		<xsl:copy-of select="node()" />
	</xsl:copy>
</xsl:template>

<xsl:template match="A | B">
	<xsl:variable name="depth" select="preceding::sect[1]/@depth" />
	
	<xsl:copy>
		<xsl:attribute name="level" select="@level + $depth"
		<xsl:apply-templates select="*" />
	</xsl:copy>
</xsl:template>

Greetings Christoph


Tom Schmitter schrieb:
I'm beginning to despair in the possibility that it may be impossible to
implement this algorithm in xslt. Perhaps one of you battle-hardened vets
could shed some light...
Input doc:
<doc>
<sect depth="3">heading</sect>
<A level="1">
<B level="2">xx</B>
</A>
<sect depth="7">heading2</sect>
<C level="1">
<D level="2">yy</D>
</C>
</doc>


Desired output:
<doc>
	<sect depth="3">heading</sect>
	<A level="4">
		<B level="5">xx</B>
	</A>
	<sect depth="7">heading2</sect>
	<C level="8">
		<D level="9">yy</D>
	</C>
</doc>

Note how the 'level' attributes in the output represent the sum of their
value in the input and the value of the 'depth' attribute of the <sect>
which precedes it.

Given the recursive processing paradigm of xslt, without the ability for a
template to return a value or to set a global variable, I can't figure out
how I can process the <sect>s and make the value of their 'depth'
attributes available for the processing of the other elements.

Would someone please tell me I'm missing something?!?

Thanks very much

Current Thread