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
    Jun 06, 2014 @ 22:17
    Steve
    0

    Listing Multiple Nested Node Names on Select

    Hi,

    I have some news articles that are stored by a node "category" and children "year". Using xslt, I am pulling in the parent node of the article, which is the year the article was published, but I would also like to pull in the category of the article as well, but I don't quite know how to go about it. Here is the section my existing xslt that is pulling the parent node of the article.

    <div class="featured-articles">
          <xsl:for-each select="$featuredArticles">
            <div class="listing">
              <p class="tag">
                <xsl:call-template name="node-name">
                  <!-- can't do ./parent::* because these nodes are copied and don't have parents -->
            <xsl:with-param name="node" select="$currentPage/ancestor-or-self::NewsHome//NewsArticle[@id=current()/@id]/parent::*" />
                </xsl:call-template>
              </p>
              
    <!-- A template to output the correct name for a given node. Better than copy/pasting this code all over the place -->
    <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>
  • Kim Nedergaard 37 posts 144 karma points
    Jun 07, 2014 @ 13:47
    Kim Nedergaard
    100

    Hi Steve,

    Just to make sure that I understand you correctly. Your sitetree looks like this:

    • Newshome
      • Category
        • Year
          • Article

    and you want to your xslt to output: Category - Year - Articlename?

    Then you could do the following:

    First find your articlenode:

    <xsl:variable name="sourceNode" select="$currentPage/ancestor-or-self::NewsHome//NewsArticle[@id=current()/@id]"/>
    

    Then call you template for the three nodes and put the output in variables to keep your markup clean:

        <!-- First category -->
    <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::category" />
             </xsl:call-template></xsl:variable>
    
        <!-- Then year -->
    <xsl:variable name="yearName">
            <xsl:call-template name="node-name">              
                    <xsl:with-param name="node" select="$currentPage/ancestor-or-self::NewsHome//NewsArticle[@id=current()/@id]/ancestor::year" />
             </xsl:call-template></xsl:variable>
    
        <!-- Finally name -->
    <xsl:variable name="articleName">
            <xsl:call-template name="node-name">              
                    <xsl:with-param name="node" select="$currentPage/ancestor-or-self::NewsHome//NewsArticle[@id=current()/@id]" />
             </xsl:call-template>
    </xsl:variable>
    
    <!-- And then output your results -->
    <xsl:value-of select="$categoryNAme"/> - <xsl:value-of select="$yearName"/> - <xsl:value-of select="$articleName"/>
    

    Hop this answers your question :)

    -Kim

  • Steve 472 posts 1216 karma points
    Jun 09, 2014 @ 15:18
    Steve
    0

    Works great Kim, but I don't understand why we even have to declare the variable "sourceNode" since we never refrence it? Could you help me understand?

  • Kim Nedergaard 37 posts 144 karma points
    Jun 10, 2014 @ 08:49
    Kim Nedergaard
    0

    Sorry, I were a bit quick on the copy+paste there :)

    Instead of calling:

    <xsl:call-template name="node-name">              
                    <xsl:with-param name="node" select="$currentPage/ancestor-or-self::NewsHome//NewsArticle[@id=current()/@id]/ancestor::year" />
    </xsl:call-template>
    

    You could call:

     <xsl:call-template name="node-name">              
                    <xsl:with-param name="node" select="$sourceNode/ancestor::year" />
     </xsl:call-template></xsl:variable>
    
  • 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