Need an XSL test to see if a loop was entered.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    Need an XSL test to see if a loop was entered.

    Hi,

    As part of an XSL translation I am trying to throw together I need to be able to tell if an "xsl:if" in a "xsl:for-each" loop is ever entered. Specificly I need to know this after the loop is completed to know if I should add a new line.

    Normally I would just change a boolean value inside the if test to check this, but XSL does not allow variables to be changed after they are set so that is not an option.

    I know people have come up with creative solutions to get around the variable limitations to make things like counters. So I was wondering if anyone knew of a creative was to do this.


    Code:
        <xsl:for-each select="//someTagName">
            <xsl:if test="@name = 'bob' ">
                <!-- Need to be able to if this if test is entered after the loop is done-->
            </xsl:if>
        </xsl:for-each>
    
        <!--
           This is where I need to know if the above test was triggered so I can know
           if I should start a new line.
        -->
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    As an alternative I realized if there was some way to know if a template was called I could use that. Since calling templates seems to start a different thread I am assuming there is no way to check. Just an idea I thought I would throw out there.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Possibly:

      1. Add after the for-each loop

      [code=xml]
      <xsl:if test="not(//someTagName)">
      Add newline here.
      </xsl:if>[/code]


      2. Put the for-each in a choose-when-otherwise clause
      [code=xml]
      <xsl:choose>
      <xsl:when test="someTagNa me">
      <xsl:for-each>
      ...
      </xsl:for-each>
      </xsl:when>
      <xsl:otherwis e>
      add Newline here
      </xsl:otherwise>
      </xsl:choose>[/code]

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Originally posted by jkmyoung

        1. Add after the for-each loop
        [code=xml]
        <xsl:if test="not(//someTagName)">
        Add newline here.
        </xsl:if>[/code]
        That will not work. That will add a new line for each tag that does not match. I only want to add one none of the elements made it though the if test.



        Originally posted by jkmyoung
        2. Put the for-each in a choose-when-otherwise clause
        [code=xml]
        <xsl:choose>
        <xsl:when test="someTagNa me">
        <xsl:for-each>
        ...
        </xsl:for-each>
        </xsl:when>
        <xsl:otherwis e>
        add Newline here
        </xsl:otherwise>
        </xsl:choose>[/code]
        This does not work either. Same problem. It will add a new line for every tag that fails the match.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Need more of your underlying xsl to see what you're doing.
          The if clause would need to be at a higher level.

          Comment

          Working...