468,532 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

xsl:if and string function

I'm at my wit's end with this one. Within an xsl:if test, I'm not able to
get 2 variables to properly evaluate if one of them is wrapped within a
string function.

<!-- This works -->
<xsl:if test="$var:v141=&quot;true&quot;">

<!-- This doesn't work -->
<xsl:if test="string($var:v141)=&quot;true&quot;">

I can't figure out why. The transformation is taking place within dotnet and
the var:v141 is assigned through a function call and it doesn't matter if I
return a boolean value or a string value ("true" or "false"), it always
evaluates to true and outputs the information wrapped within the if
statement. To further complicate things, this if statement is being
generated by the Biztalk Mapper (ugghhh!) and so I cannot edit the statement
and remove the string function.

I don't get it. Am I just overlooking something obvious here (I hope so...).

Eric

Here's the entire code block that doesn't work (I mean, regardless of the
value returned from the fctequal function, the if statment always evaluates
to true):

<xsl:for-each select="DexRaces/DexRace">
<xsl:variable name="var:v141"
select="fctequal(string(Code/text()),&quot;1&quot;)"/>
<xsl:if test="string($var:v141)=&quot;true&quot;">
<xsl:variable name="var:v142" select="&quot;true&quot;"/>
<RaceAIorANHMDA><xsl:value-of select="$var:v142"/></RaceAIorANHMDA>
</xsl:if>
</xsl:for-each>

neither version of the function helps:

public string fctequal(string value1, string value2)
{
if(value1.Equals(value2))
{
return "true";
}
return "false";
}

public bool fctequal(string value1, string value2)
{
return value1.Equals(value2)
}

Nov 12 '05 #1
3 3372
Eric Theil wrote:
<xsl:variable name="var:v141"
select="fctequal(string(Code/text()),&quot;1&quot;)"/>


This looks weird. Extension functions must be namespace prefixed. Such
stylesheet should throw
System.Xml.Xsl.XsltException: 'fctequal()' is an unknown XSLT function.
You need
user:fctequal(string(Code/text()),&quot;1&quot;)
Where "user" prefix is bound either to extension object or should be
registered in msxsl:script element:
<msxsl:script language="C#" implements-prefix="user">
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2
Oleg,

It is prefixed with the userJScript namespace, I just didn't include it in
the post.

Anyway, the function is called and I can see the output if I simply write
out the result variable. I can see the function writing out "false" if I
setup the xsl like this:

<xsl:value-of select="string($var:v141)=&quot;true&quot;"/>

but for whatever reason, the evaluation within the xsl:if block is true.

Eric

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Eric Theil wrote:
<xsl:variable name="var:v141"
select="fctequal(string(Code/text()),&quot;1&quot;)"/>


This looks weird. Extension functions must be namespace prefixed. Such
stylesheet should throw
System.Xml.Xsl.XsltException: 'fctequal()' is an unknown XSLT function.
You need
user:fctequal(string(Code/text()),&quot;1&quot;)
Where "user" prefix is bound either to extension object or should be
registered in msxsl:script element:
<msxsl:script language="C#" implements-prefix="user">
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3
Eric Theil wrote:
It is prefixed with the userJScript namespace, I just didn't include it in
the post.

Anyway, the function is called and I can see the output if I simply write
out the result variable. I can see the function writing out "false" if I
setup the xsl like this:

<xsl:value-of select="string($var:v141)=&quot;true&quot;"/>

but for whatever reason, the evaluation within the xsl:if block is true.

That's really weird. Unfortunately I cannot reproduce the problem. Just
tried the following XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:var="var"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace"
exclude-result-prefixes="var msxsl user">
<msxsl:script language="C#" implements-prefix="user">
public string fctequal(string value1, string value2)
{
if(value1.Equals(value2))
{
return "true";
}
return "false";
}
</msxsl:script>
<xsl:template match="/">
<xsl:for-each select="*">
<xsl:variable name="var:v141"
select="user:fctequal(string(text()),&quot;1&quot; )"/>
<xsl:if test="string($var:v141)=&quot;true&quot;">
<xsl:variable name="var:v142" select="&quot;true&quot;"/>
<RaceAIorANHMDA>
<xsl:value-of select="$var:v142"/>
</RaceAIorANHMDA>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

with xml:
<foo>1</foo>
it outputs "true", and with
<foo>0</foo>
it outputs nothing.

Try to get generated by BizTalk stylesheet and input XML so we can
reproduce.
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Andrea Maschio | last post: by
2 posts views Thread by Jørn Tommy Kinderås | last post: by
2 posts views Thread by websls | last post: by
5 posts views Thread by Luke Vogel | last post: by
4 posts views Thread by Doulos05 | last post: by
4 posts views Thread by grbeal | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.