tags:

views:

284

answers:

1

Hi,

I have a set of Sitecore nodes:

<item>
  <item>
    <item />    
  </item>
  <item /> <!-- (1) -->
  <item />
</item>
<item>
  <item />
  <item />
</item>

I can obtain the path of these using a function sc:path(.), which returns something like '/item/item' for the point marked (1).

What I want to be able to do is group the item based on their path.

So my output would be something like:

<ul>
  <li>in item
    <ul>
...
    </ul>
  </li>
  <li>in item/item
    <ul>
...
    </ul>
  </li>
</ul>

I am playing with the preceding axes at the moment, as in the following code:

<xsl:for-each select="exsl:node-set($processedResult)/item">
  <xsl:sort 
    select="substring-before(substring-after(sc:path(.),'/sitecore/media library/'),'.aspx')"
    data-type="text"
    order="ascending" />
  <xsl:variable 
    name="path" 
    select="search:GetFriendlyPath('/sitecore/media library/',sc:path(.))" />
    <!-- returns: item/item from /sitecore/media library/item/item/item.aspx -->           
  <xsl:variable name="lastPath">
    <xsl:choose>
      <xsl:when test="sc:path(preceding)">
        <xsl:value-of 
          select="search:GetFriendlyPath('sitecore/media library',sc:path(preceding))" />
      </xsl:when>
      <xsl:otherwise>none</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>      
  <xsl:if test="$path != $lastPath"> <!-- grouping test -->
    <li>
      <strong>in <xsl:value-of select="$path" /></strong>
    </li>
  </xsl:if>
  <li>
  <!-- render detail -->
  </li>
</xsl:for-each>

... but sc:path(preceding) returns nothing. Hence my check doesn't work.

What am I doing wrong?

+1  A: 

I'm not sure what you intend to do with

<xsl:when test="sc:path(preceding)">

This reads as "feed the children named <preceding> as a node-set to the sc:path() function".

Looking at your input, there are no child elements with that name.

Could it be you mean something like

<xsl:when test="sc:path(preceding-sibling::item[1])">

?

Knowing nothing about Sitecore, I'll give it a shot with:

<xsl:for-each select="exsl:node-set($processedResult)/item">
  <xsl:sort select="
    substring-before(
      substring-after(sc:path(.), '/sitecore/media library/'),
      '.aspx'
    )"
    data-type="text"
    order="ascending"
  />
  <xsl:variable name="path" select="
    search:GetFriendlyPath(
      '/sitecore/media library/', sc:path(.)
    )
  " />
  <xsl:variable name="lastPath">
    <xsl:choose>
      <xsl:when test="preceding-sibling::item[1]">
        <xsl:value-of select="
          search:GetFriendlyPath(
            'sitecore/media library', sc:path(preceding-sibling::item[1])
          )"
        />
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>none</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!-- grouping test -->
  <xsl:if test="$path != $lastPath">
    <li>
      <strong>
        <xsl:text>in </xsl:text>
        <xsl:value-of select="$path" />
      </strong>
    </li>
  </xsl:if>

  <li>
    <!-- render detail -->
  </li>
</xsl:for-each>
Tomalak
However, I'm not sure that the approach you take (if previous != current) is going to be successful in terms of meaningful grouping and output.
Tomalak
That's got it. Excellent, thanks very much.
Program.X
Good to hear. :)
Tomalak