Search

Hi there, was wondering if anybody could possibly help with an issues I'm having,

Okay so in short I'm trying to make a tag cloud based on inputs from the tag list field, i've created a data source to output them and the structure currently looks similar to this:

<tag-cloud>
<entry>
<tags>
<item>Tag1</item>
<item>Tag2</item>
</tags>
</entry>
<entry>
<tags>
<item>Tag3</item>
<item>Tag4</item>
</tags>
</entry>
</tag-cloud>

Now in order to spit these out I'm using the following mark-up:

<xsl:apply-templates select="//data/tag-cloud/entry"/>

<xsl:template match="//data/tag-cloud/entry">
    <xsl:apply-templates select="tags/item"/>
</xsl:template>

<xsl:template match="tags/item">
<xsl:value-of select="."/>
</xsl:template>

Now this would be fine if all I wanted was every tag submitted to list however what I'm after front end wise is if the same tag has been submitted more than once within the entire section to only display it the once and also count the number of times it has been submitted, this is so I can make some visual changes using CSS to tags that have been submitted more than once based on how many times they have been submitted.

Hope that all makes sense, like I said any help would be great, this ones got me scratching my head...

What you want is the Muenchian method for groupping. This example will get you going (empirical testing):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:exsl="http://exslt.org/common"
        extension-element-prefixes="exsl">


    <!-- Create a key on tag items -->
    <xsl:key name="tags-by-name" match="YOUR_DS_HERE/entry/tags/item" use="."/>

    <xsl:template match="/">

        <xsl:variable name="tags">

            <!-- Find the tags that appear first time -->
            <xsl:for-each select="/data/YOUR_DS_HERE/entry/tags/item[count(. | key('tags-by-name', .)[1]) = 1]">
                <tag>
                    <name>
                        <xsl:value-of select="."/>
                    </name>
                    <count>
                        <!-- Count tags with same value as current one -->
                        <xsl:value-of select="count(key('tags-by-name', .))"/>
                    </count>
                </tag>
            </xsl:for-each>

        </xsl:variable>

        <ul>
            <xsl:for-each select="exsl:node-set($tags)/tag">
                <xsl:sort select="count" data-type="number" order="descending"/>

                <li>
                    <xsl:value-of select="concat(name, ' (',count,')')"/>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>


</xsl:stylesheet>

Works a treat, thanks vladG!

Great tip vladG. Also thanks.

You're welcome. Glad it helps.

Hey this great. I have a problem though.

I'm using this to make my tag clickable:

<a href="{$root}/archive/tag/{name}">

However, if the tag has a space the page shows up blank, as they are stored with hyphens... How do I stop that?

you should probably use the handle. so instead of
<a href="{$root}/archive/tag/{name}">

you need to use
<a href="{$root}/archive/tag/{name/@handle}">

Unfortunately that seems to stop it working altogether?
Just links to /archive/tag/

Shouldn't it be name/item/@handle?

I was so hopeful that would work but no, the same result. This is a weird one.

Solved it here:

        <tag>
            <name>
                <xsl:value-of select="."/>
            </name>
            <count>
                <!-- Count tags with same value as current one -->
                <xsl:value-of select="count(key('tags-by-name', .))"/>
            </count>
            <link>
                    <xsl:value-of select="@handle"/>
             </link>
        </tag>

So...

            <a href="{$root}/archive/tag/{link}"><xsl:value-of select="concat(name, ' (',count,')')"/></a>

Easy :D

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