Simulate a boolean flag in xslt

E

Efi Merdler

Hello,
As I understand it is impossible in xslt to change the value of a
variable after you assigned a value to it.

I would like to simulate a flag. For example
if something happens in template A then update my flag.
Template B reads the flag and according to its value decides to do
something.

The main idea is that template B is independent of template A, i.e.
template A does not call it directly.

Do you have any idea ?

Thank you,
Efi
 
M

Martin Honnen

Efi said:
As I understand it is impossible in xslt to change the value of a
variable after you assigned a value to it.

That is true.
I would like to simulate a flag. For example
if something happens in template A then update my flag.
Template B reads the flag and according to its value decides to do
something.

The main idea is that template B is independent of template A, i.e.
template A does not call it directly.

You can pass parameters to templates e.g.
<xsl:apply-templates select="foo">
<xsl:with-param name="flag" select="true()"/>
</xsl:apply-templates>

<xsl:template match="foo">
<xsl:param name="flag" select="false()"/>
...
</xsl:template>

With XSLT 1.0 you would need to ensure that the parameter is passed on
to all templates involved, with XSLT 2.0 you could make use of a tunnel
parameter e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:eek:utput method="text"/>

<xsl:template match="/root">
<xsl:apply-templates select="foo">
<xsl:with-param name="flag" select="true()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:template>

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

<xsl:template match="bar">
<xsl:param name="flag" select="false()" tunnel="yes"/>
<xsl:sequence select="$flag"/>
</xsl:template>

</xsl:stylesheet>

Although the template matching foo does not have an xsl:param the tunnel
parameter named flag is passed on to the template matching bar.

There are three XSLT 2.0 implementations currently, one is Saxon from
<URL:http://saxon.sourceforge.net/>, on is Altova XML tools, one is
Gestalt <URL:http://sourceforge.net/project/showfiles.php?group_id=124274>.
 
D

Dimitre Novatchev

A different way from passing parameters (as explained by Martin Honnen), is
to define a *global* variable, whose content is the result of calling the
template named A.

Then in template B simply reference this global variable.


Cheers,
Dimitre Novatchev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top