Copied to clipboard

Flag this post as spam?

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


  • FarmFreshCode 225 posts 422 karma points
    Jun 03, 2011 @ 21:12
    FarmFreshCode
    0

    Calling Childnodes in a Loop based on a Specific Value

    Hello Everyone!
    Basic info first. I am using XSLT so far and am using Umbraco 4.7 and this is kind of a 2 parter. Please click on the images to enlarge them.

    PART 1

    First I am wanting to display all nodes of a specific type (Award Categories) on the page. You will see two "award categories" in my file tree and on my screenshot. (Outstanding Alumni & Professional Acievement)

    Then I want to list all of the Award Winners for those categories (childnodes) under each category. See the image below.

    So far I have been able to hardcode the childnode values in there but not get it to pull the actual dynamic childnodes. Here is my code so far...

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary PS.XSLTsearch ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <xsl:for-each select="umbraco.library:GetXmlNodeById(1876)/AwardCategory">
      <h2><xsl:value-of select="@nodeName"/></h2>
      <xsl:value-of select="awardCategoryDescription"/>
      <P>The <xsl:value-of select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'yyyy')"/> Award recipients are:</P>  
       
          <xsl:for-each select="umbraco.library:GetXmlNodeById(1918)/AwardWinner-Full">
            <li>
              <span style="font-weight:bold; font-size:15px;"><xsl:value-of select="@nodeName"/></span><span style="font-style:italic;"> class of '<xsl:value-of select="umbraco.library:FormatDateTime(awardWinnerGraduatingYear, 'yy')"/></span>
              <BR><xsl:value-of select="awardWinnerDepartment"/></BR>
              <xsl:value-of select="awardWinnerJobTitle"/> | <xsl:value-of select="awardWinnerCompany"/>
            </li>
          </xsl:for-each>
      <P><a href="{umbraco.library:NiceUrl(@id)}">ARCHIVE LINK</a></P><P></P>
    </xsl:for-each>

    </xsl:template>
        
    </xsl:stylesheet>

    You can see that on this line:

    <xsl:for-each select="umbraco.library:GetXmlNodeById(1918)/AwardWinner-Full">

    I have hardcoded the nodeID in there... That needs to call the childnodes of the Award Category dynamically instead.

    PART 2

    I want to be able to select a specific year of winners to display. So in the Award Category DocType I have a dropdown list of years to select from. In the image below you'll see I have 2011 selected for the "Outstanding Alumni Award" category. That means I only want to show winners in 2011 in the list.

    In the image above you'll see 2 of my sample "award winners" Todd should be displayed as he won an award in 2011 which matches the category selection. Kenneth should not show up because he only won an award in 2009.

    I used a "Tag" datatype in case the people won an award more than 1 year. Does anyone see a problem of using a dropdown list to select the date to display in the category and then matching it to a TAG value?

    Thank you for your time and help in advance.

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Jun 03, 2011 @ 22:12
    Peter Dijksterhuis
    1

    Assuming that you want those to display on the Alumni-page, you could simply use for-eaches.

    <!-- Loop through the currentPages children of type AwardCategory -->
    <xsl:for-each select="$currentPage/AwardCategory">
          <!-- Loop through the children of AwardCategory of type AwardWinner-Full -->
          <xsl:for-each select="./AwardWinner-Full">
          </xsl:for-each>
    </xsl:for-each>

     

    Does this make sense or do I not understand your structure right?

    Peter

  • FarmFreshCode 225 posts 422 karma points
    Jun 03, 2011 @ 22:31
    FarmFreshCode
    0

    I am actually posting all of this information on the Awards page. But I did use your line:

    <xsl:for-each select="./AwardWinner-Full">

    which I think will work to answer PART 1 of my issue... just the tricky is part left! ;-)
    I'll give ya a High Five though for that

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Jun 03, 2011 @ 22:36
    Peter Dijksterhuis
    1

    Part 2 shouldn't be that hard either then ;)

    <xsl:for-each select="./AwardWinner-Full[ @year = $year ]">

    @year is the property-name of the awardwinner and $year is the selected year on the parent. I'm not sure how the xml is structered on the tags, but if you do a xsl:copy-of then you'll get the entire xml spit out 

    Does this get you going? If not, post a bit xml from the copy-of so I can see the properties. I can then make a bit better xslt :)

    Peter

  • FarmFreshCode 225 posts 422 karma points
    Jun 06, 2011 @ 16:11
    FarmFreshCode
    0

    Hello Peter,

    I've been working to intergrate that second part to my script this morning but I haven't had any luck yet. I'm wondering if because my indiviual award winner is using the tags datatype that it might not be going as smoothly as I had hoped. Of course I might just be missing it too.. So here's what I got working..

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary PS.XSLTsearch ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <xsl:for-each select="umbraco.library:GetXmlNodeById(1876)/AwardCategory">
      <h2><xsl:value-of select="@nodeName"/></h2>
      <xsl:value-of select="awardCategoryDescription"/>
      <P>
        The <xsl:value-of select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'yyyy')"/> Award recipients are:
      </P
         

    <xsl:for-each select="./AwardWinner-Full">
            <li>
              <xsl:value-of select="@nodeName"/> class of '<xsl:value-of select="umbraco.library:FormatDateTime(awardWinnerGraduatingYear, 'yy')"/>
              <BR><xsl:value-of select="awardWinnerDepartment"/></BR>
              <xsl:value-of select="awardWinnerJobTitle"/> | <xsl:value-of select="awardWinnerCompany"/>
                      
            </li>
          </xsl:for-each>

     
    <P><a href="{umbraco.library:NiceUrl(@id)}">ARCHIVE LINK</a></P>
    </xsl:for-each>
    </xsl:template> 
    </xsl:stylesheet>

    The alias for the Award Category Display Year is = displayAwardsWinners (which is a dropdownlist single selection)
    and the alias for the Individual persons winning years is = awardWonTheseYears (which is a TAG dataType)

    Thanks for your help.

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 06, 2011 @ 17:42
    Tom Fulton
    1

    Hi,

    The tags datatype stores the tag as comma separated values, ie (2010,2011,2009), so you can't really do a simple equals comparison to filter.

    But you could try using the contains function instead:

    <xsl:for-each select="./AwardWinner-Full [contains(awardWonTheseYears, current()/displayAwardsWinners)]">

    Assuming displayAwardsWinners is a field on the AwardCategory document that contains the year you want to show?

    Also I'm not positive that the current() will work since you are using nested for-each loops.  If you have trouble, try dumping it into a variable first:

    <xsl:variable name="curYear" select="displayAwardsWinners"/>
    <xsl:for-each select="./AwardWinner-Full [contains(awardWonTheseYears, $curYear)]">

    Hope this helps,
    Tom

  • FarmFreshCode 225 posts 422 karma points
    Jun 06, 2011 @ 17:52
    FarmFreshCode
    0

    Yeah, looks like that did the trick. They both seem to work quite well. Thanks Tom

Please Sign in or register to post replies

Write your reply to:

Draft