Copied to clipboard

Flag this post as spam?

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


  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Aug 17, 2010 @ 10:45
    Ali Sheikh Taheri
    0

    how to validate a node before getXmlNodeById?

    How can I validate a node before getXmlNodeById?

    because if you pass a node id which is not valid it will through an exception and break the xslt.

    Any help is much appreciated 

    Ali

  • sun 403 posts 395 karma points
    Aug 17, 2010 @ 11:10
    sun
    0

    Where do you get the nodeId?

    Almost all nodeId I used are from content node. So I havn't this type problem.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Aug 17, 2010 @ 11:12
    Ali Sheikh Taheri
    0

    the problem comes into scene when an editor selects a node in content picker and after a while another editor will delete it 

    hope that makes sense.

     

  • Richard 146 posts 168 karma points
    Aug 17, 2010 @ 11:32
    Richard
    2

    My recent experience with Umbraco 4, is that GetXmlNodeById does not crash, but if you put the result into a variable it then hangs the site. If you do the following I did not experience an issue, whether the node is now unpublished or even deleted.

     

    <xsl:template match="/">
    .....
    <xsl:apply-templates select="umbraco.library:GetXmlNodeById($nodeValue)" mode="study" />

    </xsl:template>

    <xsl:template match="error" mode="study">
    <!-- Oops: <xsl:value-of select="."/> -->
    </xsl:template>

    <xsl:template match="node" mode="study">

    </xsl:template>
  • Sascha Wolter 615 posts 1101 karma points
    Aug 17, 2010 @ 12:47
    Sascha Wolter
    1

    Hi ali,

    if a node with the id can't be found something like this:

    <error>A node with that id doesn't exist</error>

    gets returned. So what I usually do is

    <xsl:variable name="myNode" select="umbraco.library:GetXmlNodeById($nodeId)" />
    <xsl:if test="count($myNode/error) = 0">
      ...
    </xsl:if>

    Doesn't obviously work with 4.5.1 if you have a doc type called 'error' (or you'd just have to check for the message inside as well). All in all though I think Richard's way is much nicer. :)

    Sascha

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Aug 17, 2010 @ 13:08
    Ali Sheikh Taheri
    0

    thanks Sascha and Richard.

    Could you help me how I can apply one of the above solution to this? as this becomes complicated with templates? 

    Thanks in advance.

    <?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:Cultiv.MediaCache="urn:Cultiv.MediaCache" 
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Cultiv.MediaCache ">
    
    
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:variable name="articles" select="$currentPage/data [@alias = 'relatedArticles']" />
    
    <xsl:template match="/">
    
    <xsl:if test="$articles != ''">
    <xsl:variable name="articlesList" select="umbraco.library:Split($articles, ',')" />
    <ul id="search-forms">
        <xsl:for-each select="$articlesList/value">
            <xsl:variable name="articleNodeId" select="." />
            <xsl:variable name="articleXml" select="umbraco.library:GetXmlNodeById($articleNodeId)" />
            <li>
                  <a href="{umbraco.library:NiceUrl($articleXml/@id)}" title="{$articleXml/data[@alias='headerText']}">
    
                <xsl:choose>
                    <xsl:when test="$articleXml/data[@alias='headerText'] != ''">
                        <xsl:value-of select="$articleXml/data[@alias='headerText']" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$articleXml/@nodeName" />
                    </xsl:otherwise>
                </xsl:choose>
    
    
                  </a>
            </li>
        </xsl:for-each>
    </ul>
    
    </xsl:if>
    
    </xsl:template>
    
    </xsl:stylesheet>
  • Sascha Wolter 615 posts 1101 karma points
    Aug 17, 2010 @ 13:19
    Sascha Wolter
    2

    Hi Ali,

    my solution would look like this:

    ...
    <xsl:variable name="articleXml" select="umbraco.library:GetXmlNodeById($articleNodeId)" />
    <xsl:if test="count($articleXml/error) = 0">
      <li>
        ...
      </li>
    </xsl:if>
    ...

    Richard's solution would then be:

    ...
    <xsl:variable name="articleXml" select="umbraco.library:GetXmlNodeById($articleNodeId)" />
    <xsl:apply-templates select="$articleXml" mode="articles"/>
    ...
    <xsl:template match="error" mode="articles">
      <!-- probably don't do anything here -->
    </xsl:template>
    <xsl:template match="node" mode="articles">
      <li>
        <a href="{umbraco.library:NiceUrl(./@id)}" title="./data[@alias='headerText']">
          ...
        </a>
      </li>
    </xsl:template>

    Hope that helps and I haven't produced too many typos,
    Sascha

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Aug 17, 2010 @ 13:24
    Ali Sheikh Taheri
    0

    wooooooow, Brilliant, great sascha.

    You are the man. Actually I choose your solution seems to be easier :D 

    Cheers

    Ali

  • Sascha Wolter 615 posts 1101 karma points
    Aug 17, 2010 @ 13:30
    Sascha Wolter
    0

    Hehe :)

    My way is probably easier if you just need a check, if you need alternative content you'd have to use a choose statement and then I would definitely go for Richard's one (also I really do like applying templates and using the mode setting, it's just more structured/separate/re-usable.

    Have a great day,
    Sascha

  • David Conlisk 432 posts 1008 karma points
    Jul 05, 2013 @ 18:12
    David Conlisk
    0

    Hi all,

    See the reference document here for more information: http://our.umbraco.org/wiki/reference/umbracolibrary/getxmlnodebyid-(1)

    Cheers!

    David

  • 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