wood burning stoves*
The moose likes XML and Related Technologies and the fly likes Need help with some conditional logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need help with some conditional logic" Watch "Need help with some conditional logic" New topic
Author

Need help with some conditional logic

Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45
Hi. Having some troubles with Xpath/XSL syntax again. Basically what I need to do is check a bunch of conditions, and only if they are all true, enter into a block.
Here's what I have:
<xsl:variable name="hasAds" select="yes"> </xsl:variable>
<xsl:for-each select="//styleinfo/car">

<xsl:if test="starts-with('/BrochureUrl', 'NA')">
<xsl:if test="starts-with('/InfoUrl', 'NA')">
<xsl:if test="starts-with('/SpecialUrl', 'NA')">
<xsl:if test="starts-with('/OfferUrl', 'NA')">
<xsl:if test="starts-with('/VideoUrl', 'NA')">
<xsl:variable name="hasAds" select="no"></xsl:variable>
</xsl:if>
</xsl:if>
</xsl:if>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:if test="starts-with('$hasAds', 'yes')">
... DO A BUNCH OF STUFF HERE...
</xsl:if>
I'm trying to create a variable called "hasAds" and set it to yes by default. If I go through that first set of <xsl:if> statments, I want the variable to be set to no. If, when it reaches this line:<xsl:if test="starts-with('$hasAds', 'yes')"> I want to enter that block. Basically, it seems that no matter what, I am never entering into the last <xsl:if> block (where I wrote "do a bunch of stuff here"), even when I know I should be.
Can anyone help out with the syntax? Thanks so much!
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Basically what I need to do is check a bunch of conditions, and only if they are all true -
if I understand your requirements right, you can simply combine your conditions with �and�:
<xsl:if test="starts-with('/BrochureUrl', 'NA') and starts-with('/InfoUrl', 'NA') and ...">
... DO A BUNCH OF STUFF HERE...
</xsl:if>
and I remember reading somewhere that the whole test=�...� sentence should be within one line (no line breaks).
Does it help?

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45
Thanks for the suggestion. The problem is, I need to do the check multiple times (for each "car" in my XML doc), but only execute the "do a bunch of stuff here" block once (or none- but not "for each"). Basically, I need to do something like this:
<xsl:for-each select="//styleinfo/car">
<xsl:if test="starts-with('/BrochureUrl', 'NA') and starts-with('/InfoUrl', 'NA') and ...">
</xsl:for-each>
... DO A BUNCH OF STUFF HERE...
</xsl:if>
But I'm pretty sure I can't nest the tags like that above. I hope I explained it clearly. Need to check for a condition in every "car", then if the condition holds, execute the "bunch of stuff" once.
Thanks for any suggestions.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Oh, I see now. The problem with XSLT variables:
1) they cannot change their value
2) their scope is the element inside which they are defined
so in
<xsl:if test="starts-with('/VideoUrl', 'NA')">
<xsl:variable name="hasAds" select="no"></xsl:variable>
</xsl:if>
you define a new variable "hasAds" which overrides �old� one and whose scope is <xsl:if> element. After </xsl:if> it goes out of scope and �old� "hasAds" with value of �yes� returns in scope.
Thinking what to do...
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
probably it should look like:
<xsl:variable name="hasAds" select="//styleinfo/car[not (starts-with('/BrochureUrl', 'NA')) or not (starts-with('/InfoUrl', 'NA') ) or ...]�">
<xsl:if test="not (hasAds)�>
... DO A BUNCH OF STUFF HERE...
</xsl:if>
<xsl:variable... selects all nodes which break your conditions. (expression in [] means "such as..."). Then <xsl:if test="not (hasAds)�> tests if hasAds is empty and if it is, does a bunch of stuff (hasAds being convert to boolean gives "false" if variable is empty, then not(false) yields true. Perhaps (count (hasAds) = 0) would be less cryptic, but conversion to boolean is said to work faster.)
Is it better now?
P.S. Actually, auxilary variable is not necessary here, just
<xsl:if test="count (//styleinfo/car[not (starts-with('/BrochureUrl', 'NA')) or not (starts-with('/InfoUrl', 'NA') ) or ...]) = 0�>
... DO A BUNCH OF STUFF HERE...
</xsl:if>

[This message has been edited by Mapraputa Is (edited May 30, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with some conditional logic
 
Similar Threads
How to add two variables in XSLT
XSL 2.0 (looking for ideas)
Transforming web.xml
XSLT Help
xsl variable and xsl:if