Copied to clipboard

Flag this post as spam?

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


  • Alex Burr 77 posts 128 karma points
    Aug 03, 2011 @ 17:45
    Alex Burr
    0

    Confusion using COUNT with XPath syntax

    Hi, I am trying to get some logic in place based on: 1. a node's position in the tree, and 2. the total number of nodes. However my COUNT() keeps returning 0.

    I have tried the following:

    <xsl:value-of select="count($currentPage/node)" />
    <xsl:value-of select="count($currentPage//node)" />
    <xsl:value-of select="count($currentPage/node [@nodeTypeAlias = 'Bio'])" />
    <xsl:value-of select="count($currentPage/nodes [@nodeTypeAlias = 'Bio'])" />

    I am consistently getting a 0 for all of these expressions and am starting to get very confused. Any help is greatly appreciated!

    FYI this is a site built on 4.7.0 so it uses the 4.5 schema.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 03, 2011 @ 17:53
    Tom Fulton
    0

    Hi Alex,

    If you are using the new schema you should be using:

    <xsl:value-ofselect="count($currentPage/Bio [@isDoc])"/>

    In the new schema there is no more <node>, each node's element is named by their Document Type Alias instead. 

    See this wiki for more info:  http://our.umbraco.org/wiki/reference/xslt/45-xml-schema

    Note this will find direct children of the type Bio, if you want all children (deep) you can use $currentPage//Bio [@isDoc]

    Hope this helps,
    Tom

  • Alex Burr 77 posts 128 karma points
    Aug 03, 2011 @ 18:27
    Alex Burr
    0

    THANK YOU TOM! That was exactly what I was looking for. I wish there were better documentation to train people on the 4.5 schema assuming no prior experience.

    Now a question. I am trying to divide my content nodes into two columns. I am using the following syntax:

    <xsl:variable name="mid" select="count($currentPage/Bio [@isDoc]) div 2"/>
      
    <section class="firstColumn">
      <xsl:for-each select="$currentPage/* [@isDoc and position() &lt;= $mid]">
      ... content ...
      
    </xsl:for-each>
    </section>
    <section class="secondColumn">
      <xsl:for-each select="$currentPage/* [@isDoc and $mid &lt;= position()]">
      ... content ...
      </xsl:for-each>
    </section>

    But the "firstColumn" is empty and everything is coming out into "secondColumn".

    Is my for-each not right?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 03, 2011 @ 20:36
    Tom Fulton
    0

    Try this instead:

    <xsl:variablename="mid"select="format-number(count($currentPage/Bio [@isDoc]) div 2,0)"/>
     
    <sectionclass="firstColumn">
      <xsl:for-eachselect="$currentPage/* [@isDoc][position() &lt;= $mid]">
      ... content ...
     
    </xsl:for-each>
    </section>
    <sectionclass="secondColumn">
      <xsl:for-eachselect="$currentPage/* [@isDoc][$mid &lt; position()]">
      ... content ...
      </xsl:for-each>
    </section>
  • Alex Burr 77 posts 128 karma points
    Aug 03, 2011 @ 21:00
    Alex Burr
    0

    Thanks for the suggestion Tom. My for-each seems to be working which leads me to suspect a caching issue.

Please Sign in or register to post replies

Write your reply to:

Draft