Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with some conditional logic

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic