469,610 Members | 1,729 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Simulate a boolean flag in xslt

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
Dec 25 '07 #1
2 6842
Efi Merdler wrote:
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:output 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>.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 26 '07 #2
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

"Efi Merdler" <fo****@gmail.comwrote in message
news:92**********************************@i29g2000 prf.googlegroups.com...
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

Dec 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bradley Plett | last post: by
16 posts views Thread by Ian Tuomi | last post: by
5 posts views Thread by vaibhav | last post: by
76 posts views Thread by KimmoA | last post: by
4 posts views Thread by moondaddy | last post: by
2 posts views Thread by Daniel | last post: by
11 posts views Thread by KiranJyothi | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.