6 users online. Create an account or sign in to join them.Users

Search

Gents, first time poster couldn’t be happier with the work everyone is involved in here.

One of my pages is a horizontal scrolling image gallery. I am trying to get the width of the containing that holds all the images to be roughly the width of the sum of the images widths.

I see that the meta information for each image is being output by symphony correctly but I’m not sure how to go about summing those to form a single value without some rough php.

Ideas?

Thanks! Austen

Hi Austen and welcome!

You could do the mathematics in JavaScript if you have a different display of images for users without JS and transform the output then with JS.
If all images have the same width it’s just a matter of multiplying the number of images with this width in your XSLT.
And if you need to sum different widths in XSLT you could use the following template

<xsl:template name="sum">
    <xsl:param name="pList"/>
    <xsl:param name="pAccum" select="0"/>

    <xsl:choose>
        <xsl:when test="$pList">
            <xsl:variable name="vHead" select="$pList[1]"/>

            <xsl:call-template name="sum">
                <xsl:with-param name="pList" select="$pList[position() > 1]"/>
                <xsl:with-param name="pAccum" select="$pAccum + $vHead/@width"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$pAccum"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

and call is with something like

<xsl:call-template name="sum">
    <xsl:with-param name="pList" select="path/to/images/file/meta"/>
</xsl:call-template>

And there might be better XSLT solutions.

Or, you could just use the sum() function:

<xsl:value-of select="sum(/data//entry/image/@width)"/>

If I remember right I had problems using the sum() function which made me using the hand made recursive sum template. But I switched to the sum() function and everything is working :)

So the sum() function is the way to go unless you want to do some fancy calculations in each step/summand.

Thanks so much for the replies,

I put this

<xsl:template match="data"><xsl:value-of select="sum(portraits/entry/image->2/@width)"/>

Using this xml

   <portraits>
    <section id="2" handle="portaits">Portaits</section>
    <entry id="1">
        <image-2 size="146 KB" path="/images/uploads" type="image/jpeg">
            <filename>_mg_8844.jpg</filename>
            <meta creation="2010-08-24T23:56:48-05:00" width="700" height="467" />
        </image-2>
    </entry>

Which gave me an output of ‘0’. Am I dense?

The width attribute is on a meta element within image-2 so your XPath would be:

portraits/entry/image-2/meta/@width

Thanks for not callin me dense, might be new to XSL but can’t believe I missed that.

Final syntax—worked great thanks so much guys.

<section id="gallery" style="width: {sum(portraits/entry/image-2/meta/@width)+100}px"

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.2 or above
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.0 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details