Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Oct 03, 2013 @ 21:00
    Steve
    0

    Test for Doctype property equals "true"

    I have a NewsArticle node that I am trying to loop through and only list the NewsArticles with the property "selectedArticle" when selectedArticle is true. Here is my code:

    <xsl:if test="count($articleNode/NewsArticle[string(data[@alias='selectedArticle']) = '1'])">
    
        <div class="block-gallery">
            <img class="logo" src="/media/870076/r_logo.png"/>
         <ul class="galery">
           <xsl:for-each select="$articleNode/NewsArticle">
           <xsl:sort select="publicationDate" order="descending" />
             <xsl:if test="position() &lt;=12">
               <xsl:apply-templates select="." />
             </xsl:if>
           </xsl:for-each>
              
  • Steve 472 posts 1216 karma points
    Oct 03, 2013 @ 21:42
    Steve
    0

    Ok, I got it to work with the code below. How would I be able to sort the output using the "sort" dropdown within Umbraco's web interface? e.g. right click on the News Article node and select "sort"

    <xsl:if test="count($articleNode/NewsArticle[selectedArticle=1]) &gt; 0">
    
    <div class="block-gallery">
        <img class="logo" src="/media/870076/r_logo.png"/>
     <ul class="galery">
         <xsl:for-each select="$articleNode/descendant::NewsArticle[selectedArticle = 1]">
       <xsl:sort select="publicationDate" order="descending" />
         <xsl:if test="position() &lt;=12">
           <xsl:apply-templates select="." />
         </xsl:if>
       </xsl:for-each>
    
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 03, 2013 @ 21:52
    Chriztian Steinmeier
    0

    Hi Steve,

    The code you've used is mixing the two schemas - here's a tidied version using a variable to avoid duplicated XPaths (the path you use in the if statement should be the same you use in the for-each, otherwise you can easily get unexpected results):

    <xsl:variable name="selectedArticles" select="$articleNode/NewsArticle[selectedArticle = 1]" />
    
    <xsl:if test="$selectedArticles">
        <div class="block-gallery">
            <img class="logo" src="/media/870076/r_logo.png" />
            <ul class="galery">
                <xsl:for-each select="$selectedArticles">
                    <xsl:sort select="publicationDate" order="descending" />
                    <xsl:if test="position() <= 12">
                        <xsl:apply-templates select="." />
                    </xsl:if>
                </xsl:for-each>
            </ul>
        </div>
    </xsl:if>
    

    /Chriztian

    EDIT: I see you found the schema-bug in the original if statement - that's what I was referring to; Apparently I was writing this while you were posting the second question :-)

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 03, 2013 @ 21:55
    Chriztian Steinmeier
    0

    Steve,

    I'm not sure what you mean regarding the sort thing - could you elaborate? Do you want to sort the nodes in the backoffice?

    /Chriztian

  • Steve 472 posts 1216 karma points
    Oct 03, 2013 @ 22:02
    Steve
    0

    Chriztian,

    Thanks, but I need the descendants of the NewsCategory[3]. Sorry, I didn't include that in my previous messages. And never mind the sorting- figured it out.

    <xsl:variable name="articleNode" select="$siteRoot/NewsHome/NewsCategory[3]" />
    
    
    
    <xsl:if test="count($articleNode/NewsArticle[selectedArticle=1]) &gt; 0">
    
    <div class="block-gallery">
        <img class="logo" src="/media/870076/r_logo.png"/>
     <ul class="galery">
         <xsl:for-each select="$articleNode/descendant::NewsArticle[selectedArticle = 1]">
    <!--   <xsl:sort select="publicationDate" order="descending" /> -->
         <xsl:if test="position() &lt;=12">
           <xsl:apply-templates select="." />
         </xsl:if>
       </xsl:for-each> 
    
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 03, 2013 @ 22:21
    Chriztian Steinmeier
    0

    Okay - no worries:-)

    You should still create a variable for the nodes - because what you're doing now can give you unexpected results; In the if statement you're checking if any of the children have the selectedArticle property checked, but then in the for-each, you're using the descendant:: axis - to avoid having to remember to duplicate the XPath, put it in a variable straight away.

    The bonus to that is also that you can perform a simple boolean test on that variable (which is in fact a nodeset - even if it's an empty one) to test whether there's any nodes - an empty nodeset will return false().

    So (the double-slash // is a shortcut for descendant::):

    <xsl:variable name="selectedArticles" select="$articleNode//NewsArticle[selectedArticle = 1]" />
    
    <xsl:if test="$selectedArticles">
        <div class="block-gallery">
            <img class="logo" src="/media/870076/r_logo.png" />
            <ul class="galery">
                <xsl:for-each select="$selectedArticles">
                    <xsl:if test="position() &lt;= 12">
                        <xsl:apply-templates select="." />
                    </xsl:if>
                </xsl:for-each>
            </ul>
        </div>
    </xsl:if>
    

    BTW: If you're sorting them manually in the backoffice, you can skip the for-each and just apply templates directly using the same position predicate:

    <xsl:if test="$selectedArticles">
        <div class="block-gallery">
            <img class="logo" src="/media/870076/r_logo.png" />
            <ul class="galery">
                <xsl:apply-templates select="$selectedArticles[position() &lt;= 12]" />
            </ul>
        </div>
    </xsl:if>
    

    /Chriztian

  • Steve 472 posts 1216 karma points
    Oct 03, 2013 @ 22:44
    Steve
    0

    Cool! Here is what I've got now, and it seems to be working! Thanks Chriztian!

    <xsl:if test="$selectedArticles">
    
    <div class="block-gallery">
        <img class="logo" src="/media/870076/r_logo.png"/>
     <ul class="galery">
         <xsl:for-each select="$selectedArticles">
    <!--   <xsl:sort select="publicationDate" order="descending" /> -->
         <xsl:if test="position() &lt;=12">
           <xsl:apply-templates select="." />
         </xsl:if>
       </xsl:for-each>
    
Please Sign in or register to post replies

Write your reply to:

Draft