Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kasper Dyrvig 246 posts 379 karma points
    Sep 08, 2011 @ 07:27
    Kasper Dyrvig
    0

    Insert xpath parameter but only if querystring not equal nothing or "all"

    Maybe someone has asked this before, but I don't know what to search for. I want to create a for-each loop. But if the querystring "sortCountry" is not equal to nothing or to "all" then the for-each loop must find only the specifired countries. Does that make sence?

    I'll demonstrate... This is code to be fired if the sortCountry != '' or sortCountry != 'all':

    <xsl:for-each select="umbraco.library:GetXmlDocumentByUrl($productFeedUrl, 3600)//Product [Country = umbraco.library:RequestQueryString('sortCountry')]">
      <!-- Something funny happens -->
    </xsl:for-each>

    Otherwise:

    <xsl:for-each select="umbraco.library:GetXmlDocumentByUrl($productFeedUrl, 3600)//Product">
      <!-- Something less funny happens -->
    </xsl:for-each>

    Well, I kind of answared my own question here... I could use a choose. Let me hear your suggestions. Thanks

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 08, 2011 @ 08:46
    Fuji Kusaka
    0

    Hi kasper you could do something like

    <xsl:for-eachselect="umbraco.library:GetXmlDocumentByUrl($productFeedUrl, 3600)//Product [Country = umbraco.library:RequestQueryString('sortCountry')]">
     
    <!-- Something funny happens -->
      <xsl:choose>
          <xsl:when test="sortCountry != ''">
                         
          </xsl:when>
      <xsl:otherwise>
        <!-- Something Else will happpen -->
        
    </xsl:otherwise>
    </xsl:choose>
    </xsl:for-each>
  • Kasper Dyrvig 246 posts 379 karma points
    Sep 08, 2011 @ 11:24
    Kasper Dyrvig
    0

    Hm... that dosen't make much sense to me. You suggest I loop thrugh the products WITH the parameter and inside the loop tells what to do WITHOUT the parameter...?

    Is there a way to add a parameter to the for-each if sortCountry contains something?

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 08, 2011 @ 11:30
    Fuji Kusaka
    0

    Sorry what i wanted to suggest you was to choose make something like

     

    <xsl:choose>
          <xsl:whentest="sortCountry != ''">
                   <!-- somthing funny happens -->  
       
    <!-- Here you could also call a template where you will have your loop -->
          </xsl:when>
      <xsl:otherwise>
        <!-- Something Less Funny happpens -->
    <!-- another template where you will have another loop -->    
    </xsl:otherwise>
    </xsl:choose>
  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 08, 2011 @ 11:46
    Chriztian Steinmeier
    1

    Hi Kasper,

    The best way to test stuff like this is with the normalize-space() function:

    <!-- Grab the query param -->
    <xsl:variable name="sortCountry" select="umbraco.library:RequestQueryString('sortCountry')" />
    
    <!-- Fetch the products -->
    <xsl:variable name="products" select="umbraco.library:GetXmlDocumentByUrl($productFeedUrl, 3600)//Product" />
    
    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="normalize-space($sortCountry) and not($sortCountry = 'all')">
                <xsl:apply-templates select="$products[Country = $sortCountry]" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="$products" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <!-- Template for a Product -->
    <xsl:template match="Product">
        <!-- Do something -->
    </xsl:template>
    

    Also, take note of how to reduce duplication by using variables for intermediate steps...

    /Chriztian

  • Kasper Dyrvig 246 posts 379 karma points
    Sep 09, 2011 @ 09:00
    Kasper Dyrvig
    0

    Hi Chriztian

    Good point with the variables.

    Just to be sure of what normalize-space() does... if the variable contains let say: " this     little test text  ", will it then remove the spaces completely ("thislittletesttext") or just the spaces that isn't needed ("this little test text")?

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 09, 2011 @ 09:12
    Chriztian Steinmeier
    1

    Hi Kasper,

    normalize-space() removes leading and trailing whitespace, and converts runs of whitespace into a single space - imagine this:

    <bodyText>    Here I am
    so for       the   demoing issue...
    
    we can say.
    
    
    </bodyText>

    Then normalize-space(bodyText) would give you:

    "Here I am so for the demoing issue... we can say."

    Which is excellent for use in e.g. HTML attributes.

    If the string starts as only whitespace, normalize-space() will return an empty string - and when you perform a test="" in XSLT the result will be sent through the boolean() function - and boolean(EMPTY STRING HERE) will return false(), so I've found it to be the best way to test if an element (or a variable) actually contains something.

    (I know that was a way longer answer than necessary - hope it helps, though :-)

    /Chriztian

  • Kasper Dyrvig 246 posts 379 karma points
    Sep 09, 2011 @ 09:19
    Kasper Dyrvig
    0

    It stands very clear for me now ;-)

    Thanks for you both for your responses. Have a nice day

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies