Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    May 18, 2015 @ 17:00
    Steve
    0

    XPath Confusion

    I have a macro that is pulling in nodes that have been selected from a tree picker into a type of news feed for the page that it is placed on. I am trying to add the value of the parent of the "NewsArticle", which would be the "NewsCategory" and output the value of the category to the individual items in the "NewsFeed".

    Here is the xslt, but I am not getting any output from the value-of select statement.

    <xsl:variable name="categoryName">
        <xsl:call-template name="node-name">        
        <xsl:with-param name="node" select="$currentPage/ancestor-or-self::NewsHome//NewsArticle[@id=current()/@id]/ancestor::NewsCategory" />
             </xsl:call-template></xsl:variable><xsl:value-of select="$categoryName" />
    <xsl:template name="node-name">
      <xsl:param name="node" />  
      <xsl:if test="string($node) != ''">
        <xsl:choose>
          <xsl:when test="$node/pageTitle != ''">
            <xsl:value-of select="$node/pageTitle"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$node/@nodeName"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </xsl:template>
    
  • Chriztian Steinmeier 2733 posts 8352 karma points MVP 5x admin c-trib
    May 18, 2015 @ 17:37
    Chriztian Steinmeier
    0

    Hi Steve,

    If your NewsCategory nodes are just simple nodes with only the pageTitle property, only categories that have the pageTitle set will output anything (because the code is testing the string value of the $node, which is all of its text() node children).

    First, change the test to test for a node instead of its value:

    <xsl:template name="node-name">
        <xsl:param name="node" />
        <xsl:if test="$node">
            <xsl:choose>
                <xsl:when test="$node/pageTitle != ''">
                    <xsl:value-of select="$node/pageTitle"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$node/@nodeName"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    

    Provided your picked nodes exist (and are published) you should now see some output.

    Now, you could try refactoring the code to get rid of the choose altogether:

    <xsl:template name="node-name">
        <xsl:param name="node" />
        <xsl:if test="$node">
            <xsl:value-of select="($node/@nodeName[not(normalize-space(../pageTitle))] | $node/pageTitle)[1]" />
        </xsl:if>
    </xsl:template>
    

    If this doesn't work, we should have a look at how you're getting the ids for the picked nodes...

    /Chriztian

  • Steve 472 posts 1216 karma points
    May 18, 2015 @ 17:48
    Steve
    0

    Nope, unfortunatly that didn't produce any output.

    The structure is: (nodes are by document type)

    <root>

    <Academics>
    <SomeAcademicItem>
    <SomeAcademicNews></SomeAcademicNews>
    </SomeAcademicItem>
    <Academics>

    <NewsHome>
    <NewsCategory>
    <NewsArticle></NewsArticle>
    </NewsCategory>
    </NewsHome> 

    </root>

  • Steve 472 posts 1216 karma points
    May 18, 2015 @ 18:06
    Steve
    0

    Also, the macro that is pulling the NewsCategory items in, is placed on the "SomeAcademicNews" page, if that makes a difference.

    I thought the xpath using $currentNode/ancestor-or-self:: would have gotten out to the root, doesn't it?

  • Steve 472 posts 1216 karma points
    May 18, 2015 @ 18:22
    Steve
    0

    I've got it working with this xpath, but as I said I thought it would be already starting from the root with $currentPage/ancestor-or-self.

    <xsl:with-param name="node" select="$currentPage/ancestor-or-self::*/NewsHome//NewsArticle[@id=current()/@id]/ancestor::NewsCategory" />
  • Chriztian Steinmeier 2733 posts 8352 karma points MVP 5x admin c-trib
    May 18, 2015 @ 18:28
    Chriztian Steinmeier
    100

    Hi Steve,

    Ah - that's it then.

    I thought the xpath using $currentNode/ancestor-or-self:: would have gotten out to the root, doesn't it?

    Nope - that won't do, because the NewsHome node is not an ancestor of the SomeAcademicNews node - but here's the fix:

    Create a variable at the top level - e.g. just after the currentPage param:

    <xsl:param name="currentPage" />
    <xsl:variable name="absRoot" select="$currentPage/ancestor::root" />
    

    Then use that as the base when selecting the category:

    <xsl:with-param name="node" select="$absRoot/NewsHome//NewsArticle[@id=current()/@id]/ancestor::NewsCategory" />
    

    Let me know if that does it,

    /Chriztian

  • Steve 472 posts 1216 karma points
    May 18, 2015 @ 18:39
    Steve
    0

    That works!! I guess I just don't understand xpath axies.

  • Steve 472 posts 1216 karma points
    May 18, 2015 @ 18:49
    Steve
    0

    I always thought that $currentPage/ancestor::Homepage would first get the listing of ancestors of the currentPage that are on the Homepage axis, is that the correct way of thinking?

  • Chriztian Steinmeier 2733 posts 8352 karma points MVP 5x admin c-trib
    May 18, 2015 @ 19:41
    Chriztian Steinmeier
    0

    Awesome - great that it works.

    Not quite - the "axis" is the ancestor[-or-self]:: part - Homepage is what you're looking for along that axis. Have a play with the XPath Axes Visualizer to get familiar with what's selected... :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft