Copied to clipboard

Flag this post as spam?

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


  • Hundebol 167 posts 314 karma points
    Dec 16, 2009 @ 13:46
    Hundebol
    0

    Address a specific nodetypealias

    Hi!

    On a projekt i'm working on, I have created a Job-section. In this job section i have a count function in an xslt file. This XSLT counts and displays how many job adverts there are currently posted.

    This works great, but when i want to have the counter on the frontpage, this does not work.

    My tree looks like this, if this i for any help

    Frontpage (the count function does not work here)
    -- About
    --- HR
    ---- Job Section          (The count-function works here)
    ----- Job advert 1
    ----- Job advert 1
    ----- etc.

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

        <xsl:param name="currentPage"/>
        <xsl:param name="MaxNoChars" select="150" />
        <xsl:variable name="level" select="4"/>
        <xsl:template match="/">


    <xsl:choose>
    <!--IF THERE ARE NO JOBS AVAILABLE -->
    <xsl:when test="count($currentPage/descendant-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &lt;= 0">
    <p>We currently have no vacant jobs</p>
    </xsl:when>
    <xsl:otherwise>
        <xsl:choose>
        <!-- IF THE NUMBER OF JOBS IS 1 -->
        <xsl:when test="count($currentPage/descendant-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &lt;= 1">
        <p>We currently have <xsl:value-of select="count($currentPage/descendant-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1'])"/> vacant job</p>
        </xsl:when>
        <xsl:otherwise>
        <!-- IF THE NUMBER OF JOBS IS HIGHER THAN 1 - E.G 2,3,4 ETC. -->
        <xsl:if test="count($currentPage/descendant-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &gt;= 1">
        <p>We currently have <xsl:value-of select="count($currentPage/descendant-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1'])"/> vacant jobs</p>
        </xsl:if>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:otherwise>
    </xsl:choose>


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

    My problem is that, the code on the frontpage counts all nodes on level 4, where it should only count the children of the Job Section. But how?

    I have tried some (failed) attempts with nodetypealias - but nothing works.

    NB: If any other suggestions to my XSLT, please post - i know it's not that "best practice".

    best regards,
    Hundebol

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Dec 16, 2009 @ 14:13
    Douglas Robar
    0

    I'd go for something like this...

     

    <xsl:variable name="jobCount" select="count(
    $currentPage/ancestor-or-self::node [@level=1]/descendant-or-self::node [
    @nodeTypeAlias='jobPosting'
    and string(data[@alias='umbracoNaviHide']) != '1')
    ]" />

    <xsl:choose>

    <!--IF THERE ARE NO JOBS AVAILABLE -->
    <xsl:when test="$jobCount &lt;= 0">
    <p>We currently have no vacant jobs</p>
    </xsl:when>
    <xsl:otherwise>
       
    <xsl:choose>
       
    <!-- IF THE NUMBER OF JOBS IS 1 -->
       
    <xsl:when test="$jobCount = 1">
       
    <p>We currently have <xsl:value-of select="$jobCount"/> vacant job</p>
       
    </xsl:when>
       
    <xsl:otherwise>
       
    <!-- IF THE NUMBER OF JOBS IS HIGHER THAN 1 - E.G 2,3,4 ETC. -->
       
    <xsl:if test="$jobCount &gt;= 1">
       
    <p>We currently have <xsl:value-of select="$jobCount"/> vacant jobs</p>
       
    </xsl:if>
       
    </xsl:otherwise>
       
    </xsl:choose>

    </xsl:otherwise>
    </xsl:choose>

     

    Two things to notice.

    First, a variable with the number of vacant jobs means the calculation only has to be done once (better performance) and is easier to edit if you need to.

    Second, the jobCount variable works from anywhere in the content tree by going UP the tree to the homepage and then back down, looking for any pages of the 'jobPosting' document Type. You'll need to change the document type alias according to what you really called the docType of the job nodes.

     

    cheers,
    doug.

  • Hundebol 167 posts 314 karma points
    Dec 16, 2009 @ 14:43
    Hundebol
    0

    Hi Doug,

    Thank you for your great help - i'm sure your xslt is much better than mine. I knew i had too many "request" in my choose-sentence.

    Unfortunately things are still not working.

    It shows, "We currently have no vacant jobs" no matter how many nodes i have published.

    My doctype alias is JobAdvert and i have changed that, but nothing has really changed.

    Any other good ideas?

     

    best regards,
    hundebol

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Dec 16, 2009 @ 15:41
    Douglas Robar
    0

    I had a typo in my original post (the last closing parenthesis and square bracket should have been in the opposite positions).

    Even so, time for a bit of step-by-step debugging...

    Open your xslt file (or create a new one without a matching macro) and add the following line:

    <xsl:value-of select="count(
    umbraco.library:GetXmlAll()//node [@nodeTypeAlias='JobAdvert']
    )" />

    This is a short-hand approach to counting ALL the published 'jobAdvert' nodes in your site no matter where they are.

    umbraco.library:GetXmlAll() is equivalent to $currentPage/ancestor-or-self::node[@level=1] for your site structure (if you ever have multiple websites in this one umbraco installation you'll want to return to the longer expression.

    The '//' is the short-hand way of writing descendant::node and in your site structure is valid since JobAdverts always appear somewhere below the home page.

     

    Once you've pasted in this xsl:value-of statement, highlight those lines and click the 'Visual XSLT' icon on the toolbar. You don't even need to save the xslt file first!

    You should see the count returned as something other than zero.

     

    If so, you can add the check for the 'umbracoNaviHide' setting back in:

    <xsl:value-of select="count(
    umbraco.library:GetXmlAll()//node [@nodeTypeAlias='JobAdvert'
    and string(data[@alias='umbracoNaviHide']) != '' ]
    )" />

     

    Visualize that again and you'll get the count of avaliable JobAdverts.

    Once that's working properly you can simply update your xslt's jobCount variable with this same select= statement and you should be all set.

    <xsl:variable name="jobCount" select="count(
    umbraco.library:GetXmlAll()//node [@nodeTypeAlias='JobAdvert'
    and string(data[@alias='umbracoNaviHide']) != '' ]
    )" />

    For comparison, the long-hand and slightly more resiliant alternative would be:

    <xsl:value-of select="count(
    $currentPage/ancestor-or-self::node [@level=1]/
    descendant-or-self::node [@nodeTypeAlias='JobAdvert'
    and string(data[@alias='umbracoNaviHide']) != '' ]
    )" />

     

    cheers,
    doug.

  • Hundebol 167 posts 314 karma points
    Dec 16, 2009 @ 16:33
    Hundebol
    0

    Hi Doug,

    Thanks for your step-by-step - thats just the kind of help one is looking for. I had also seen & corrected your typo in the first post, so that wasn't it.

    I must however say, that things are not working. The code is correct, but, the two last snippets return a zero while the two getXmlAll-snippets work like a charm and return the correct "2".

    You mentioned that if i had more than one website in an installation i could not use the getXmlAll - an that is just the case here actually. I have a danish and an english version of the site.

    But i can not understand why the two last does not work - you have just turned it into a variable, but apparently thats enough to break it!?

    Hope you have more ideas, and many thanks for your help so far!

    br/

    hundebol

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Dec 16, 2009 @ 17:52
    Douglas Robar
    0

    Sorry, it's just one brain cramp after another... you should check for string(data[@alias='umbracoNaviHide'] != '1' rather than != ''

    The thing about GetXmlAll() is that it gets all the content nodes for both English and Danish. If you want to restrict it to just the nodes in the site that you're website visitor is currently viewing then you should use $currentPage, walk up the content tree to the homepage, and then look from there down for the job adverts. That will let you use the same xpath no matter what site you're in.

    Given the typos and mental lapses in my posts to you there's a chance I've still got a typo... but that's the logic and why GetXmlAll() may not be the way you want to go since you've got multiple sites in one content tree.

    Make sense?

    cheers,
    doug.

  • Hundebol 167 posts 314 karma points
    Dec 18, 2009 @ 10:15
    Hundebol
    0

    Hi Doug!

    One more time i'd like to thank you for your help. It's much appreciated!

    I have the following working on my test domain right now:

    <?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"
        exclude-result-prefixes="msxml umbraco.library">

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

        <xsl:param name="currentPage"/>
        <xsl:variable name="level" select="4"/>
        <xsl:template match="/">

    <xsl:variable name="jobCount" select="count($currentPage//node [@level=$level]/node [@nodeTypeAlias='JobAdvert' and string(data[@alias='umbracoNaviHide']) != '1'])" />

    <xsl:choose>
    <!--IF THERE ARE NO JOBS AVAILABLE -->
    <xsl:when test="$jobCount &lt;= 0">
    <p>We currently have no vacant jobs</p>
    </xsl:when>
    <xsl:otherwise>
        <xsl:choose>
        <!-- IF THE NUMBER OF JOBS IS 1 -->
        <xsl:when test="$jobCount = 1">
        <p>We currently have <xsl:value-of select="$jobCount"/> vacant job</p>
        </xsl:when>
        <xsl:otherwise>
        <!-- IF THE NUMBER OF JOBS IS HIGHER THAN 1 - E.G 2,3,4 ETC. -->
        <xsl:if test="$jobCount &gt;= 1">
        <p>We currently have <xsl:value-of select="$jobCount"/> vacant jobs</p>
        </xsl:if>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:otherwise>
    </xsl:choose>

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

    But as soon as i put the Macro in, on "the real" frontpage, things does not work.

    A little elaboration on my current tree:

    Frontpage Denmark
    -- About
    --- HR
    ---- Job Section
    ----- Job advert 1
    ----- Job advert 1

    Frontpage United Kingdom
    -- About
    --- HR
    ---- Job Section
    ----- Job advert 1
    ----- Job advert 1

    Frontpage "Testing-area"
    -- About
    --- HR
    ---- Job Section
    ----- Job advert 1
    ----- Job advert 1

    This is how my tree looks - but the XSLT above, only works on the "Testing area", where it counts just fine! I'm about to pull out my hair on this one - can't figure it out. (When saying the it does not work on DK & UK - it means; just showing "currently no jobs", even though there is 1 job published)

    Hope you have more bright ideas!

    best regards
    hundebol

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Dec 18, 2009 @ 11:33
    Douglas Robar
    0

    Sure seems like it should work. It almost has to be something silly that, once you find it, will be obvious but right now is baffling.

    If you would like me to spend a couple minutes looking at the site (contact me through my website and give me a login to your site) and maybe I can find it.

    cheers,
    doug.

  • Hundebol 167 posts 314 karma points
    Dec 18, 2009 @ 13:08
    Hundebol
    0

    Hi Doug!

    Finally! And of course - at the precise moment i read your post, i knew what was wrong - the "good old trick" - re-publish the tree. Then it worked!

    Thanks a bunch for all your help - and all your theory about why things work the way they do! That's an invaluable help.

    For the record, the code below is my final working code. What it does is, it displays a link on my frontpage, telling the visitor if there are any vacant Jobs available. If there are, it displays how many. The Job Section (DocTypeAlias Job Section) is on level 4 - the job adverts (DocTypeAlias Job Advert) is it's children.

    <?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"
        exclude-result-prefixes="msxml umbraco.library">

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

        <xsl:param name="currentPage"/>
        <xsl:variable name="level" select="4"/>
        <xsl:template match="/">


    <xsl:variable name="jobCount" select="count($currentPage//node [@level=$level]/node [@nodeTypeAlias='JobAdvert' and string(data[@alias='umbracoNaviHide']) != '1'])" />

    <xsl:choose>
    <!--IF THERE ARE NO JOBS AVAILABLE -->
    <xsl:when test="$jobCount &lt;= 0">
    <p>&gt; <a href="/om-bw/human-resources/ledige-stillinger.aspx">Vi har i øjeblikket ingen ledige stillinger</a></p>
    </xsl:when>
    <xsl:otherwise>
        <xsl:choose>
        <!-- IF THE NUMBER OF JOBS IS 1 -->
        <xsl:when test="$jobCount = 1">
        <p>&gt; <a href="/om-bw/human-resources/ledige-stillinger.aspx">Vi har i øjeblikket <xsl:value-of select="$jobCount"/> ledig stilling</a></p>
        </xsl:when>
        <xsl:otherwise>
        <!-- IF THE NUMBER OF JOBS IS HIGHER THAN 1 - E.G 2,3,4 ETC. -->
        <xsl:if test="$jobCount &gt;= 1">
        <p>&gt; <a href="/om-bw/human-resources/ledige-stillinger.aspx">Vi har i øjeblikket <xsl:value-of select="$jobCount"/> ledige stillinger</a></p>
        </xsl:if>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:otherwise>
    </xsl:choose>

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

     

    Best regards
    hundebol

Please Sign in or register to post replies

Write your reply to:

Draft