String-compare functions?

Discussion in 'XML' started by Nasos Makriyiannis, Jul 24, 2003.

  1. Hi,

    I'm new to XSL and I was wondering if there is a string-compare function
    available. I am using the following IF statement but it does not seem to be
    working:

    <xsl:if test="boolean(normalize-space(name(..))='Stateflow')">
    ....do something...
    </xsl:if>
    <xsl:if test="boolean(normalize-space(name(..)) != 'Stateflow')">
    ....do another thing...
    </xsl:if>

    One of the above two cases should be true but neither of them are called. I
    displayed the value-of name(..) and it comes out as expected (i.e. sometimes
    it's 'Stateflow' and other times it's not!).

    I also tried using the contain() function: <xsl:if
    test="contains(name(..),'Stateflow') = 'true'"> but that does not work
    either. Again I viewed the value-of contains(name(..),'Stateflow') and it
    does come out as true.

    Any ides why the none of the IF statements are run?

    thanks,

    -Nasos
     
    Nasos Makriyiannis, Jul 24, 2003
    #1
    1. Advertising

  2. Could you, please, provide a complete (but minimal example) -- the
    source.xml, your xslt code.


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL



    "Nasos Makriyiannis" <> wrote in message
    news:bfp4u6$c1m$...
    > Hi,
    >
    > I'm new to XSL and I was wondering if there is a string-compare function
    > available. I am using the following IF statement but it does not seem to

    be
    > working:
    >
    > <xsl:if test="boolean(normalize-space(name(..))='Stateflow')">
    > ...do something...
    > </xsl:if>
    > <xsl:if test="boolean(normalize-space(name(..)) != 'Stateflow')">
    > ...do another thing...
    > </xsl:if>
    >
    > One of the above two cases should be true but neither of them are called.

    I
    > displayed the value-of name(..) and it comes out as expected (i.e.

    sometimes
    > it's 'Stateflow' and other times it's not!).
    >
    > I also tried using the contain() function: <xsl:if
    > test="contains(name(..),'Stateflow') = 'true'"> but that does not work
    > either. Again I viewed the value-of contains(name(..),'Stateflow') and it
    > does come out as true.
    >
    > Any ides why the none of the IF statements are run?
    >
    > thanks,
    >
    > -Nasos
    >
    >
     
    Dimitre Novatchev, Jul 24, 2003
    #2
    1. Advertising

  3. Sure thing. Do you have an e-mail where I can send you the files? I'm having
    trouble attaching documents to the newsgroup.

    I'll provide an explanation in the e-mail also.

    regards,

    -Nasos

    "Dimitre Novatchev" <> wrote in message
    news:bfp7mi$grhs0$-berlin.de...
    > Could you, please, provide a complete (but minimal example) -- the
    > source.xml, your xslt code.
    >
    >
    > =====
    > Cheers,
    >
    > Dimitre Novatchev.
    > http://fxsl.sourceforge.net/ -- the home of FXSL
    >
    >
    >
    > "Nasos Makriyiannis" <> wrote in message
    > news:bfp4u6$c1m$...
    > > Hi,
    > >
    > > I'm new to XSL and I was wondering if there is a string-compare function
    > > available. I am using the following IF statement but it does not seem to

    > be
    > > working:
    > >
    > > <xsl:if test="boolean(normalize-space(name(..))='Stateflow')">
    > > ...do something...
    > > </xsl:if>
    > > <xsl:if test="boolean(normalize-space(name(..)) != 'Stateflow')">
    > > ...do another thing...
    > > </xsl:if>
    > >
    > > One of the above two cases should be true but neither of them are

    called.
    > I
    > > displayed the value-of name(..) and it comes out as expected (i.e.

    > sometimes
    > > it's 'Stateflow' and other times it's not!).
    > >
    > > I also tried using the contain() function: <xsl:if
    > > test="contains(name(..),'Stateflow') = 'true'"> but that does not work
    > > either. Again I viewed the value-of contains(name(..),'Stateflow') and

    it
    > > does come out as true.
    > >
    > > Any ides why the none of the IF statements are run?
    > >
    > > thanks,
    > >
    > > -Nasos
    > >
    > >

    >
    >
     
    Nasos Makriyiannis, Jul 24, 2003
    #3
  4. OK here it is (I sent you 2 files to your Yahoo address so please disregard
    them):

    ===XML====

    <?xml version="1.0" ?>
    <METRICSINFORMATION>
    <Stateflow>
    <chartName>chart_constant</chartName>
    <numTransition>DIFFTAG_rem 3 DIFFTAG_add 4</numTransition>
    <transitionData>
    <labelString>?</labelString>
    </transitionData>
    </Stateflow>
    </METRICSINFORMATION>

    ===============


    ====XSL=========

    <?xml version="1.0"?>

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


    <xsl:template match="/">
    <html>
    <head>
    <basefont face="Arial" size="4" />
    </head>
    <body>
    <ol>
    <xsl:apply-templates select="/METRICSINFORMATION/Stateflow"/>
    </ol>
    </body>
    </html>
    </xsl:template>


    <xsl:template name="Stateflow" match="/METRICSINFORMATION/Stateflow">

    <xsl:for-each select="*">

    <xsl:call-template name="GlobalTemplateStateflow"/>

    </xsl:for-each>

    <xsl:for-each select="*/*">

    <xsl:call-template name="GlobalTemplateStateflow"/>

    </xsl:for-each>
    </xsl:template>


    <xsl:template name = "GlobalTemplateStateflow" match='/METRICSINFORMATION'>


    <xsl:if test="boolean(contains(name(..),'Stateflow'))">
    <xsl:variable name="chartNameBB" select="../chartName"/>
    <xsl:variable name="chartName"
    select="normalize-space($chartNameBB)"/>
    </xsl:if>
    <xsl:if test="boolean(contains(name,'Stateflow'))">
    <xsl:variable name="chartNameBB" select="../../chartName"/>
    <xsl:variable name="chartName"
    select="normalize-space($chartNameBB)"/>
    <xsl:variable name="machineN" select="../../machineName"/>
    <xsl:variable name="machineName"
    select="normalize-space($machineN)"/>
    </xsl:if>

    <li><xsl:value-of select="$chartName"/></li>


    </xsl:template>




    </xsl:stylesheet>

    ========================================

    thanks,

    -Nasos

    "Dimitre Novatchev" <> wrote in message
    news:bfpc6r$hduii$-berlin.de...
    > Can't you just put them inline in your message?
    >
    >
    > =====
    > Cheers,
    >
    > Dimitre Novatchev.
    > http://fxsl.sourceforge.net/ -- the home of FXSL
    >
    > "Nasos Makriyiannis" <> wrote in message
    > news:bfp9he$l7q$...
    > > Sure thing. Do you have an e-mail where I can send you the files? I'm

    > having
    > > trouble attaching documents to the newsgroup.
    > >
    > > I'll provide an explanation in the e-mail also.
    > >
    > > regards,
    > >
    > > -Nasos
    > >
    > > "Dimitre Novatchev" <> wrote in message
    > > news:bfp7mi$grhs0$-berlin.de...
    > > > Could you, please, provide a complete (but minimal example) -- the
    > > > source.xml, your xslt code.
    > > >
    > > >
    > > > =====
    > > > Cheers,
    > > >
    > > > Dimitre Novatchev.
    > > > http://fxsl.sourceforge.net/ -- the home of FXSL
    > > >
    > > >
    > > >
    > > > "Nasos Makriyiannis" <> wrote in message
    > > > news:bfp4u6$c1m$...
    > > > > Hi,
    > > > >
    > > > > I'm new to XSL and I was wondering if there is a string-compare

    > function
    > > > > available. I am using the following IF statement but it does not

    seem
    > to
    > > > be
    > > > > working:
    > > > >
    > > > > <xsl:if test="boolean(normalize-space(name(..))='Stateflow')">
    > > > > ...do something...
    > > > > </xsl:if>
    > > > > <xsl:if test="boolean(normalize-space(name(..)) != 'Stateflow')">
    > > > > ...do another thing...
    > > > > </xsl:if>
    > > > >
    > > > > One of the above two cases should be true but neither of them are

    > > called.
    > > > I
    > > > > displayed the value-of name(..) and it comes out as expected (i.e.
    > > > sometimes
    > > > > it's 'Stateflow' and other times it's not!).
    > > > >
    > > > > I also tried using the contain() function: <xsl:if
    > > > > test="contains(name(..),'Stateflow') = 'true'"> but that does not

    work
    > > > > either. Again I viewed the value-of contains(name(..),'Stateflow')

    > and
    > > it
    > > > > does come out as true.
    > > > >
    > > > > Any ides why the none of the IF statements are run?
    > > > >
    > > > > thanks,
    > > > >
    > > > > -Nasos
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Nasos Makriyiannis, Jul 24, 2003
    #4
  5. The problem has nothing to do with string comparison.

    You define incorrectly your xsl:variables and they immediately go out of
    scope.

    Then, when you try to use the variable $chartName, it is out of scope (not
    defined at all), so MSXML4 issues the following error message:

    Error occurred while compiling stylesheet '(Untitled)'.

    Code: 0x80004005
    A reference to variable or parameter 'chartName' cannot be resolved. The
    variable or parameter may not be defined, or it may not be in scope.



    In fact, this is a very FAQ:

    If you want to define a xsl:variable so that its value is determined based
    on different conditions, the correct way to do this is:

    <xsl:variable name="comeVar">
    <xsl:choose>
    <xsl:when test="cond1">value1</xsl:when>
    <xsl:when test="cond2">value2</xsl:when>
    . . . . .
    <xsl:when test="condN">valueN</xsl:when>
    </xsl:choose>
    </xsl:variable>



    Hope this helped.


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL

    "Nasos Makriyiannis" <> wrote in message
    news:bfpcok$sul$...
    > OK here it is (I sent you 2 files to your Yahoo address so please

    disregard
    > them):
    >
    > ===XML====
    >
    > <?xml version="1.0" ?>
    > <METRICSINFORMATION>
    > <Stateflow>
    > <chartName>chart_constant</chartName>
    > <numTransition>DIFFTAG_rem 3 DIFFTAG_add 4</numTransition>
    > <transitionData>
    > <labelString>?</labelString>
    > </transitionData>
    > </Stateflow>
    > </METRICSINFORMATION>
    >
    > ===============
    >
    >
    > ====XSL=========
    >
    > <?xml version="1.0"?>
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    >
    > <xsl:template match="/">
    > <html>
    > <head>
    > <basefont face="Arial" size="4" />
    > </head>
    > <body>
    > <ol>
    > <xsl:apply-templates select="/METRICSINFORMATION/Stateflow"/>
    > </ol>
    > </body>
    > </html>
    > </xsl:template>
    >
    >
    > <xsl:template name="Stateflow" match="/METRICSINFORMATION/Stateflow">
    >
    > <xsl:for-each select="*">
    >
    > <xsl:call-template name="GlobalTemplateStateflow"/>
    >
    > </xsl:for-each>
    >
    > <xsl:for-each select="*/*">
    >
    > <xsl:call-template name="GlobalTemplateStateflow"/>
    >
    > </xsl:for-each>
    > </xsl:template>
    >
    >
    > <xsl:template name = "GlobalTemplateStateflow"

    match='/METRICSINFORMATION'>
    >
    >
    > <xsl:if test="boolean(contains(name(..),'Stateflow'))">
    > <xsl:variable name="chartNameBB" select="../chartName"/>
    > <xsl:variable name="chartName"
    > select="normalize-space($chartNameBB)"/>
    > </xsl:if>
    > <xsl:if test="boolean(contains(name,'Stateflow'))">
    > <xsl:variable name="chartNameBB" select="../../chartName"/>
    > <xsl:variable name="chartName"
    > select="normalize-space($chartNameBB)"/>
    > <xsl:variable name="machineN" select="../../machineName"/>
    > <xsl:variable name="machineName"
    > select="normalize-space($machineN)"/>
    > </xsl:if>
    >
    > <li><xsl:value-of select="$chartName"/></li>
    >
    >
    > </xsl:template>
    >
    >
    >
    >
    > </xsl:stylesheet>
    >
    > ========================================
    >
    > thanks,
    >
    > -Nasos
    >
    > "Dimitre Novatchev" <> wrote in message
    > news:bfpc6r$hduii$-berlin.de...
    > > Can't you just put them inline in your message?
    > >
    > >
    > > =====
    > > Cheers,
    > >
    > > Dimitre Novatchev.
    > > http://fxsl.sourceforge.net/ -- the home of FXSL
    > >
    > > "Nasos Makriyiannis" <> wrote in message
    > > news:bfp9he$l7q$...
    > > > Sure thing. Do you have an e-mail where I can send you the files? I'm

    > > having
    > > > trouble attaching documents to the newsgroup.
    > > >
    > > > I'll provide an explanation in the e-mail also.
    > > >
    > > > regards,
    > > >
    > > > -Nasos
    > > >
    > > > "Dimitre Novatchev" <> wrote in message
    > > > news:bfp7mi$grhs0$-berlin.de...
    > > > > Could you, please, provide a complete (but minimal example) -- the
    > > > > source.xml, your xslt code.
    > > > >
    > > > >
    > > > > =====
    > > > > Cheers,
    > > > >
    > > > > Dimitre Novatchev.
    > > > > http://fxsl.sourceforge.net/ -- the home of FXSL
    > > > >
    > > > >
    > > > >
    > > > > "Nasos Makriyiannis" <> wrote in message
    > > > > news:bfp4u6$c1m$...
    > > > > > Hi,
    > > > > >
    > > > > > I'm new to XSL and I was wondering if there is a string-compare

    > > function
    > > > > > available. I am using the following IF statement but it does not

    > seem
    > > to
    > > > > be
    > > > > > working:
    > > > > >
    > > > > > <xsl:if test="boolean(normalize-space(name(..))='Stateflow')">
    > > > > > ...do something...
    > > > > > </xsl:if>
    > > > > > <xsl:if test="boolean(normalize-space(name(..)) != 'Stateflow')">
    > > > > > ...do another thing...
    > > > > > </xsl:if>
    > > > > >
    > > > > > One of the above two cases should be true but neither of them are
    > > > called.
    > > > > I
    > > > > > displayed the value-of name(..) and it comes out as expected (i.e.
    > > > > sometimes
    > > > > > it's 'Stateflow' and other times it's not!).
    > > > > >
    > > > > > I also tried using the contain() function: <xsl:if
    > > > > > test="contains(name(..),'Stateflow') = 'true'"> but that does not

    > work
    > > > > > either. Again I viewed the value-of

    contains(name(..),'Stateflow')
    > > and
    > > > it
    > > > > > does come out as true.
    > > > > >
    > > > > > Any ides why the none of the IF statements are run?
    > > > > >
    > > > > > thanks,
    > > > > >
    > > > > > -Nasos
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Dimitre Novatchev, Jul 24, 2003
    #5
  6. Thanks so much for you time Dimitre! I modified my code and it seems to be
    working now.

    Best wishes,

    -Nasos

    "Dimitre Novatchev" <> wrote in message
    news:bfpfn8$hhfci$-berlin.de...
    > The problem has nothing to do with string comparison.
    >
    > You define incorrectly your xsl:variables and they immediately go out of
    > scope.
    >
    > Then, when you try to use the variable $chartName, it is out of scope (not
    > defined at all), so MSXML4 issues the following error message:
    >
    > Error occurred while compiling stylesheet '(Untitled)'.
    >
    > Code: 0x80004005
    > A reference to variable or parameter 'chartName' cannot be resolved. The
    > variable or parameter may not be defined, or it may not be in scope.
    >
    >
    >
    > In fact, this is a very FAQ:
    >
    > If you want to define a xsl:variable so that its value is determined based
    > on different conditions, the correct way to do this is:
    >
    > <xsl:variable name="comeVar">
    > <xsl:choose>
    > <xsl:when test="cond1">value1</xsl:when>
    > <xsl:when test="cond2">value2</xsl:when>
    > . . . . .
    > <xsl:when test="condN">valueN</xsl:when>
    > </xsl:choose>
    > </xsl:variable>
    >
    >
    >
    > Hope this helped.
    >
    >
    > =====
    > Cheers,
    >
    > Dimitre Novatchev.
    > http://fxsl.sourceforge.net/ -- the home of FXSL
    >
    > "Nasos Makriyiannis" <> wrote in message
    > news:bfpcok$sul$...
    > > OK here it is (I sent you 2 files to your Yahoo address so please

    > disregard
    > > them):
    > >
    > > ===XML====
    > >
    > > <?xml version="1.0" ?>
    > > <METRICSINFORMATION>
    > > <Stateflow>
    > > <chartName>chart_constant</chartName>
    > > <numTransition>DIFFTAG_rem 3 DIFFTAG_add 4</numTransition>
    > > <transitionData>
    > > <labelString>?</labelString>
    > > </transitionData>
    > > </Stateflow>
    > > </METRICSINFORMATION>
    > >
    > > ===============
    > >
    > >
    > > ====XSL=========
    > >
    > > <?xml version="1.0"?>
    > >
    > > <xsl:stylesheet version="1.0"
    > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > >
    > >
    > > <xsl:template match="/">
    > > <html>
    > > <head>
    > > <basefont face="Arial" size="4" />
    > > </head>
    > > <body>
    > > <ol>
    > > <xsl:apply-templates select="/METRICSINFORMATION/Stateflow"/>
    > > </ol>
    > > </body>
    > > </html>
    > > </xsl:template>
    > >
    > >
    > > <xsl:template name="Stateflow" match="/METRICSINFORMATION/Stateflow">
    > >
    > > <xsl:for-each select="*">
    > >
    > > <xsl:call-template name="GlobalTemplateStateflow"/>
    > >
    > > </xsl:for-each>
    > >
    > > <xsl:for-each select="*/*">
    > >
    > > <xsl:call-template name="GlobalTemplateStateflow"/>
    > >
    > > </xsl:for-each>
    > > </xsl:template>
    > >
    > >
    > > <xsl:template name = "GlobalTemplateStateflow"

    > match='/METRICSINFORMATION'>
    > >
    > >
    > > <xsl:if test="boolean(contains(name(..),'Stateflow'))">
    > > <xsl:variable name="chartNameBB" select="../chartName"/>
    > > <xsl:variable name="chartName"
    > > select="normalize-space($chartNameBB)"/>
    > > </xsl:if>
    > > <xsl:if test="boolean(contains(name,'Stateflow'))">
    > > <xsl:variable name="chartNameBB" select="../../chartName"/>
    > > <xsl:variable name="chartName"
    > > select="normalize-space($chartNameBB)"/>
    > > <xsl:variable name="machineN" select="../../machineName"/>
    > > <xsl:variable name="machineName"
    > > select="normalize-space($machineN)"/>
    > > </xsl:if>
    > >
    > > <li><xsl:value-of select="$chartName"/></li>
    > >
    > >
    > > </xsl:template>
    > >
    > >
    > >
    > >
    > > </xsl:stylesheet>
    > >
    > > ========================================
    > >
    > > thanks,
    > >
    > > -Nasos
    > >
    > > "Dimitre Novatchev" <> wrote in message
    > > news:bfpc6r$hduii$-berlin.de...
    > > > Can't you just put them inline in your message?
    > > >
    > > >
    > > > =====
    > > > Cheers,
    > > >
    > > > Dimitre Novatchev.
    > > > http://fxsl.sourceforge.net/ -- the home of FXSL
    > > >
    > > > "Nasos Makriyiannis" <> wrote in message
    > > > news:bfp9he$l7q$...
    > > > > Sure thing. Do you have an e-mail where I can send you the files?

    I'm
    > > > having
    > > > > trouble attaching documents to the newsgroup.
    > > > >
    > > > > I'll provide an explanation in the e-mail also.
    > > > >
    > > > > regards,
    > > > >
    > > > > -Nasos
    > > > >
    > > > > "Dimitre Novatchev" <> wrote in message
    > > > > news:bfp7mi$grhs0$-berlin.de...
    > > > > > Could you, please, provide a complete (but minimal example) -- the
    > > > > > source.xml, your xslt code.
    > > > > >
    > > > > >
    > > > > > =====
    > > > > > Cheers,
    > > > > >
    > > > > > Dimitre Novatchev.
    > > > > > http://fxsl.sourceforge.net/ -- the home of FXSL
    > > > > >
    > > > > >
    > > > > >
    > > > > > "Nasos Makriyiannis" <> wrote in message
    > > > > > news:bfp4u6$c1m$...
    > > > > > > Hi,
    > > > > > >
    > > > > > > I'm new to XSL and I was wondering if there is a string-compare
    > > > function
    > > > > > > available. I am using the following IF statement but it does not

    > > seem
    > > > to
    > > > > > be
    > > > > > > working:
    > > > > > >
    > > > > > > <xsl:if test="boolean(normalize-space(name(..))='Stateflow')">
    > > > > > > ...do something...
    > > > > > > </xsl:if>
    > > > > > > <xsl:if test="boolean(normalize-space(name(..)) !=

    'Stateflow')">
    > > > > > > ...do another thing...
    > > > > > > </xsl:if>
    > > > > > >
    > > > > > > One of the above two cases should be true but neither of them

    are
    > > > > called.
    > > > > > I
    > > > > > > displayed the value-of name(..) and it comes out as expected

    (i.e.
    > > > > > sometimes
    > > > > > > it's 'Stateflow' and other times it's not!).
    > > > > > >
    > > > > > > I also tried using the contain() function: <xsl:if
    > > > > > > test="contains(name(..),'Stateflow') = 'true'"> but that does

    not
    > > work
    > > > > > > either. Again I viewed the value-of

    > contains(name(..),'Stateflow')
    > > > and
    > > > > it
    > > > > > > does come out as true.
    > > > > > >
    > > > > > > Any ides why the none of the IF statements are run?
    > > > > > >
    > > > > > > thanks,
    > > > > > >
    > > > > > > -Nasos
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Nasos Makriyiannis, Jul 24, 2003
    #6
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,666
    Victor Bazarov
    Jun 21, 2004
  2. Tony Johansson
    Replies:
    3
    Views:
    2,191
    Jack Klein
    Dec 16, 2004
  3. Replies:
    10
    Views:
    806
  4. Replies:
    10
    Views:
    812
    Roland Pibinger
    Jan 24, 2007
  5. Replies:
    2
    Views:
    478
Loading...

Share This Page