Copied to clipboard

Flag this post as spam?

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


  • Chad Rosenthal 272 posts 474 karma points
    Aug 03, 2010 @ 23:40
    Chad Rosenthal
    0

    Get current node's type in 4.5 schema

    I'm feeling really dumb right now. I can loop through all of the children based on the type, but can't figure out how to do it based on the current item.

    The XML for my current item is: 

    <NewsItem id="1091" parentID="1090" level="4" writerID="0" creatorID="0" nodeType="1089" template="1084" sortOrder="1" createDate="2010-08-03T16:35:46" updateDate="2010-08-03T16:36:14" nodeName="Test Article" urlName="test-article" writerName="Administrator" creatorName="Administrator" path="-1,1053,1068,1090,1091" isDoc=""><mainHeader>This is the test article</mainHeader><displayDate>2010-08-03T00:00:00</displayDate><summary>dsafasdf asd fasdf</summary><fullArticle>
    <p>asd fasd fasdf asdf</p>
    </fullArticle><externalHyperlink /><file /><umbracoRedirect /><navigationOverride></navigationOverride><umbracoExternalRedirect /><umbracoUrlAlias /><umbracoUrlName /><umbracoNaviHide>0</umbracoNaviHide><pageTitle /><removeSitename>0</removeSitename><metaKeywords></metaKeywords><metaDescription></metaDescription><copyright /></NewsItem>

    I want to check if the current node's name is NewsItem. If it is....display one template, otherwise do a different template.

    Now that I look at it, I should just check the NodeType, but I've spent so much time trying to figure this out....that I just want to know the answer now.

    Any help is appreciated.

     

  • Alex Norcliffe 222 posts 287 karma points
    Aug 03, 2010 @ 23:45
    Alex Norcliffe
    0

    Hi Chad, Please could you give an example of the context (e.g. XSLT) where you're trying? E.g., you could use the name() function in XPath, or apply a template matching the node name

  • webangelo 107 posts 190 karma points
    Aug 03, 2010 @ 23:52
    webangelo
    0

    Chad,

    Try the following:

    <xsl:if test="name() = 'NewsItem'">

    --Chris

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 04, 2010 @ 00:34
    Chriztian Steinmeier
    1

    Hi Chad,

    You can either do what Chris suggests, or you can use test the self:: axis, like this:

    <xsl:if test="self::NewsItem">

    (My personal belief is that an axis check performs better than a call to name(), but that's probably of no real concern to anybody :-) 

    /Chriztian

  • Chad Rosenthal 272 posts 474 karma points
    Aug 04, 2010 @ 01:13
    Chad Rosenthal
    0
    So this code worked:
    <xsl:if test="$currentNode/self::NewsItem">

    while this did not:

    <xsl:if test="$currentNode/name() = 'NewsItem'">

     

    Now just out of curiosity, is there a way to retrieve the name if you don't know it? Say I was trying to compare two nodes to each other (I know I can use the nodetype id number, but I'm trying to get how this XSLT works.

    Ie. You can't just type $currentNode/name(); That error's out. Basically what I'm trying to do is this:

                <xsl:when test="$currentNode[name()] = $itemTemplate">
                    1<xsl:apply-templates select="$currentNode" mode="single" />
                </xsl:when>
                <!-- Current Item is a Container for News Items -->
                <xsl:otherwise>

    </xsl:otherwise>

    Yet the if statement that works, can't be used in this context since you need to be able to pull the value to compare it to the variable $itemTemplate. Again, the smart thing to do at this point is change how $itemTemplate works and change it to $itemTemplateId and boom...this would be done and easy. But how would I ever learn if I always did the easy thing? Right?

    Thanks for the answers so far.

    -C

     

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 04, 2010 @ 01:50
    Chriztian Steinmeier
    1

    Hi again,

    You can use name() like that - you just need to do it right :-)

    <xsl:when test="$currentNode[name() = $itemTemplate]">
        <xsl:apply-templates select="$currentNode" mode="single" />
    </xsl:when>
    <xsl:otherwise>
        <!-- other stuff -->
    </xsl:otherwise>

    /Chriztian

  • Chad Rosenthal 272 posts 474 karma points
    Aug 04, 2010 @ 02:00
    Chad Rosenthal
    0

    I just tried it and I get an XSLT error. Just says 'false'.

    -C

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 04, 2010 @ 02:06
    Chriztian Steinmeier
    0

    Okay - how do you set $currentNode ?

    /Chriztian

  • Chad Rosenthal 272 posts 474 karma points
    Aug 04, 2010 @ 02:45
    Chad Rosenthal
    0
          <xsl:choose>
                <xsl:when test="$startingNode != ''">
                    <xsl:variable name="currentNode" select="umbraco.library:GetXmlNodeById($startingNode )"></xsl:variable>
                    <xsl:call-template name="mainFunctionality">
                        <xsl:with-param name="currentNode" select="$currentNode" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="currentNode" select="$currentPage"></xsl:variable>
                    <xsl:call-template name="mainFunctionality">
                        <xsl:with-param name="currentNode" select="$currentNode" />
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 04, 2010 @ 09:02
    Chriztian Steinmeier
    0

    Hi Chad,

    OK - just to be sure - how do you set $itemTemplate? I'm guessing something like this:

    <xsl:variable name="itemTemplate" select=" 'NewsItem' " />

    (The extra spaces aren't necessary - just there to clarify the need for the apostrophes which are required.)

    This is easily confused when ping-pong'ing between self::NewsItem (where you're NOT using a string) and testing the result of name() (where you ARE using a string).  

    /Chriztian

  • Chad Rosenthal 272 posts 474 karma points
    Aug 04, 2010 @ 16:46
    Chad Rosenthal
    0

    Ok. It's working today. 

    Thanks for all of your help!

    -C

     

Please Sign in or register to post replies

Write your reply to:

Draft