Search

It's throwing an XSLT error for an invalid expression. But the logic is exactly what I'm trying to achieve. At least I now know that no variables are needed. Thanks a lot for helping. I'll try working around this, but let me know if you have suggestions on how to fix it.

PS. I didn't include all XML because only the stories and ratings are causing me trouble. The ratings-options is there and I use it to count the maximum rating available, but no problems with that.

Still having trouble with this.

If I use the following:

<a style="width: {18*(format-number(round(100*(sum(/data/ratings/entry/rating/@handle) div count(/data/ratings/entry))) div 100, '##.00'))}px;">

all ratings are included in the calculation. But since on the page there is a list of stories I have to bind the ratings to each story, otherwise all stories appear with the same rating.

Stephen's suggestion

<a style="width: {18*(format-number(round(100*(sum(/data/ratings/entry/[@story-id = current()/@id]rating/@handle) div count(/data/ratings/entry[@story-id = current()/@id]))) div 100, '##.00'))}px;">

is exactly what I need (logically) but it's throwing an invalid expression error. Please, help me figure out what's wrong.

PS. The whole template is pasted in previous posts and is called from inside stories/entry.

Sorry, ellie. I led you astray by giving you code with syntax errors :-(

I tested this time. With the following XML:

<?xml version='1.0' encoding='utf-8'?>
<data>
    <stories>
        <entry id="1">
            <title handle="story-title-1">Story Title 1</title>
        </entry>
        <entry id="2">
            <title handle="story-title-2">Story Title 2</title>
        </entry>
    </stories>
    <ratings>
        <entry>
            <story-id handle="1">1</story-id>
            <rating handle="4">4</rating>
        </entry>
        <entry>
            <story-id handle="1">1</story-id>
            <rating handle="6">6</rating>
        </entry>
        <entry>
            <story-id handle="2">2</story-id>
            <rating handle="9">9</rating>
        </entry>
    </ratings>
</data>

I can use this XSLT:

<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='html' version='1.0' encoding='utf-8' indent='yes'/>

<xsl:template match="/">
    <xsl:apply-templates select="data" />
</xsl:template>

<xsl:template match="data">
    <div id="storyCurrentRating">
        <ul class="storyCurrentRating">
            <xsl:apply-templates select="stories/entry" />
        </ul>
    </div>
</xsl:template>

<xsl:template match="stories/entry">
    <li><a style="width: {18*(format-number(round(100*(sum(/data/ratings/entry[story-id = current()/@id]/rating/@handle) div count(/data/ratings/entry[story-id = current()/@id]))) div 100, '##.00'))}px;"><xsl:value-of select="title" /></a></li>
</xsl:template>

</xsl:stylesheet>

To output this result:

<div id="storyCurrentRating">
  <ul class="storyCurrentRating">
    <li>
      <a style="width: 90px;">Story Title 1</a>
    </li>
    <li>
      <a style="width: 162px;">Story Title 2</a>
    </li>
  </ul>
</div>

Hopefully that helps.

You made my day... Thank you, Stephen!

And you certainly helped a lot even yesterday as I stopped wasting time with variables.

Now each story has the correct rating. Strangely, in the individual story view (where I'm calling the same template) I have to use the old version (without [story-id = current()/@id]) but it's not a problem!

Thanks again! I can now move on to the next part of adding raters in the play.

EDIT: Ignore what I said about not being able to use the same template for individual story view - I was calling it from the wrong place and it didn't know how to find current()/@id. Fixed now and the system is perfect.

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