Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 10, 2009 @ 12:26
    Kim Andersen
    0

    Possible to check if a node is published or not in XSLT?

    On one of my sites, the user can use some of the same content on a lot of different pages, so the same content can be used over and over againg, without creating the same content more than one time.

    But if a node is choosen on another node, and the imported node is unpublished, the site layout f***'s up, because the imported, unpublished, nodeID still figures as one of the nodes who should be rendered on the site. On all of my pages, I use the multiple tree picker, to choose the nodes that can be imported on that page.

    But is there some way in Umbraco/XSLT to find out, if a node is published or not?

     

    I hope this makes sense to you folks. It actually work pretty perfect, and is easy for the webmasters to use. Just a little difficult to describe in words :D

     

  • Chris Koiak 700 posts 2626 karma points
    Nov 10, 2009 @ 12:57
    Chris Koiak
    0

    If a node exists in the XML then it is published. You could always check if the node exists with something like

    <xsl:variable name="nodeId" select="1180"/>
    <xsl:if test="count($currentPage/ancestor-or-self::node[@level=1]//node[@id=$nodeId]) > 0">
    </xsl:if>

    or

    <xsl:variable name="nodeId" select="1180"/>
    <xsl:if test="count(umbraco.library:GetXmlNodeById($nodeId)) > 0">
    </xsl:if>
  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Nov 10, 2009 @ 12:59
    Douglas Robar
    0

    XSLT *only* has access to published nodes, so when you request a node id that is now unpublished it will either give an error or fail silently. My guess is that you have a for-each loop on all of the node id's. You'll just want to check that each nodeid actually has some content in it... which means it is currently published.

    I can think of a couple ways to do this. One would be to check a propertie's content, such as bodyText. Another would be to get the NiceUrl(). Another would be to do a GetXmlNodeById(). I'm sure there are others.

    The important thing is to check the result with the string() != '' syntax one see's everywhere. This will ensure the macro doesn't give an error if the node isn't published.

    Shout if you need a code sample.

    cheers,
    doug.

  • Ronnie Hegelund 48 posts 710 karma points
    Nov 10, 2009 @ 13:00
    Ronnie Hegelund
    0

    OR

    <xsl:if test="string-length(umbraco.library:GetXmlNodeById('1088')/error) &gt; 0">
    Error
    </xsl:if>

     

     

    :-)

  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 11, 2009 @ 21:19
    Kim Andersen
    0

    Thanks. I'll try out your examples in one of the days :)

  • Gary 20 posts 44 karma points
    Nov 13, 2009 @ 03:44
    Gary
    0

    I had the same problem I've tried Chris' example and it seems not to work for what I was doing. eg

    <xsl:if test="count(umbraco.library:GetXmlNodeById($nodeId)) > 0">

    This seems to be because GetXmlNodeById() will return something like "The is no published content for node xxx" so the count will be 1 regardless.

    I did not try Ronnie's method though as I just modified the above to test

    <xsl:if test="umbraco.library:GetXmlNodeById(./@link)/data [@alias='yourAlias'] != ''>

    instead as I was also looking to see if the property was empty even if it was published (I am using the related links datatype and needed to check if content was available on the linked pages). This does work.

    Hope it helps :)

     

Please Sign in or register to post replies

Write your reply to:

Draft