Simulate a boolean flag in xslt

Discussion in 'XML' started by Efi Merdler, Dec 25, 2007.

  1. Efi Merdler

    Efi Merdler Guest

    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
     
    Efi Merdler, Dec 25, 2007
    #1
    1. Advertising

  2. 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: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>.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 26, 2007
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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
     
    Dimitre Novatchev, Dec 26, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    761
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,744
    Dimitre Novatchev
    Sep 25, 2003
  3. Daniel

    boolean flag vs threading.Event

    Daniel, Feb 27, 2007, in forum: Python
    Replies:
    2
    Views:
    370
    Daniel
    Feb 28, 2007
  4. J Leonard
    Replies:
    4
    Views:
    12,878
    Mark Space
    Jan 19, 2008
  5. Metre Meter
    Replies:
    7
    Views:
    454
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page