Search

I have 3 types

Products, Categories, Subcategories

Categories are related to subcategories by the parent field extension Products are related to Categories by a Tag list

I am trying to do a count of products under each category

I have a data source which outputs a list of each product and its category,

<ds-count-products-by-category>
  <item handle="compressors">compressors</item>
  <item handle="compressors">compressors</item>
  <item handle="complete-plants">complete-plants</item>
  <item handle="complete-plants">complete-plants</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
  <item handle="chillers">chillers</item>
</ds-count-products-by-category>

I then tried doing a count in XSLT,

<xsl:param name="category" select="entry/title/@handle" />
        <xsl:variable name="category_count">
            <xsl:value-of select="count(//data/params/ds-count-products-by-category/item[@handle = '$category'])"/>

        </xsl:variable>

Assigning that to a variable and using that

<xsl:value-of select="$category_count"/>

It doesnt work, anyway I feel there must be an easier way to acheive what I'm after any pointers?

Nb. Just to clarify when I say it doesnt work

<xsl:value-of select="chillers"/>

will work so it seems as though the variable wont be assigned

I just solved it ...

I had assigned the variable outside of a for loop... but I still feel there must be a simpler way to do a count maybe with a combined data source?

There are a couple of issues with your example, but without knowing the full picture of how you're iterating over the data, I can only give you more generic solutions to your problem.

Here's a method of counting nodes using xsl:key: http://xpathr.com/view/4071524/

You don't have to do count() using keys but this does make things a little simpler.

Hi Allen thanks for the reply! I didnt know about xpathr so that looks a great help

although I have it working, xsl:key does make things a lot simpler

thanks again for your reply

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details