Search

I’m still new to XSLT, so I’m curious if I can approach these two things better:

<a>
    <xsl:attribute name="href">
        <xsl:value-of select="$root" />
        <xsl:text>/</xsl:text>
        <xsl:value-of select="$root-page" />
        <xsl:text>/</xsl:text>
        <xsl:value-of select="$year - 1" />
    </xsl:attribute>
    More from last year...
</a>

The best I could come up with is:

<xsl:variable name="previous-year" select="$year - 1" />
<a href="{$root}/{$root-page}/{$previous-year}">More from last year...</a>

Is there a way to do the calculation right inside the href?

One more question:

<xsl:apply-templates select="/data/article-images[entry/article/item/@id=$entry-id]" mode="frontpage"/>

So I have a selectbox link field setup for the article images section, and a DS attached to the page (that has a bunch of articles) that outputs any images that are attached to any of the articles, but to access them I have to use the above to make it work. It seems fine, but it occurs to me there might be a better way of organizing things, be it through the sections, the page, or the DS, or the apply-templates.

Any input would be welcome!

I’m not sure if this is what you’re asking, but you could simply process the calculation within the attribute value template (inside the curly braces):

<a href="{$root}/{$root-page}/{$year - 1}">More from last year...</a>

I tried doing that earlier, but it didn’t work! Now, it magically does, perhaps I had a typo hidden amidst the chaos.

Thanks!

A slightly different way is to use the XSLT string concat() function, which accepts a comma separated list of strings to concatenate:

<a href="{concat($root,'/',$root-page,'/',$year - 1)}">More from last year...</a>

You may have received an error had to tried $year-1 (without spaces) since XSLT would try to find the variable of the name year-1 rather than subtract 1 from the variable year :-)

As for your other question — nope, this is likely the cleanest way of doing things. So long as the article-images Data Source is filtering out images only for the articles shown on that page (using an output parameter from the Articles DS), then there’s probably no easier way of doing it.

Thanks Nick,

What about dynamic variables, like when using eval in PHP?

eval(‘$’.”$year”.’-‘.”$bleh”)

Perhaps, you could try the evaluate() function from EXSLT. But you wouldn’t need to. In fact, I’ve yet to come across a scenario where this is required.

http://www.exslt.org/dyn/functions/evaluate/index.html

@nickdunn

I’ve used EXSL evaluate() to allow parsing attribute value templates (aka ‘inside the curly braces’) in a source XML doc. Example

I tried to find your thread earlier to reference but couldn’t. Thnaks for digging it up :-)

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