Search

I am trying to get some width/height values based on an image src value. I currently have this xml:

<post-images>
    <section id="14" handle="post-images">Post Images</section>
    <post link-id="120" link-handle="this-is-sal-making-a-test-post">
        <entry id="121">
            <image size="77 kb" path="/uploads/posts" type="image/jpg">
                <filename>peek-1265068218.jpg</filename>
                <meta creation="2010-02-01T18:50:18-05:00" width="485" height="271" />
            </image>
            <description word-count="0" mode="formatted" />
            <width handle="100">100</width>
            <height handle="100">100</height>
        </entry>
    </post>
</post-images>

and i want to select the width and height that are a child of the entry. I tried this xsl:

<xsl:param name="entry" select="1" />
<xsl:param name="img-val" select="substring-after(., '/posts/')" />

<xsl:param name="w">
    <xsl:for-each select="//post-images/post[@link-id = $entry]/entry">
        <xsl:choose>
            <xsl:when test="$img-val = image/filename">
                <xsl:value-of select="width" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="400" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:param>

but this line:

<xsl:when test="$img-val = image/filename/">

that does not match anything - it comes back as false so it grabs the default value. When i remove $img-val =, that statement:

<xsl:when test="image/filename">

works and so it grabs my correct width value.

Does anyone know why i cannot do a test two different values in this manner?

What’s the context of your xsl template: what’s your current node when selecting substring-after(., '/posts/')?

forget the exact context, but in this scenario, we’ll say it does evaluate to a value (which it does in the real one). But since I’m using //post-images, the context doesn’t really matter. I think i’m narrowing down the issue, though. It seems like it’s only happening on posts that have just one image in it. Haven’t finished my testing though.

ha…so i wasn’t going crazy - turned out it was the wrong freaking image! testing things out and one image got uploaded twice, but at different times. so with the unique upload field extension, it got me confused for a very long time!

anyway, that above does work, but here’s a more succinct way of writing it:

<xsl:param name="w">
    <xsl:choose>
        <xsl:when test="$img-val = //post-images/post[@link-id = $entry]/entry/image/filename">
            <xsl:value-of select="//post-images/post[@link-id = $entry]/entry[image/filename = $img-val]/width" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="450" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:param>

this is assuming there is a width (and height if you choose so) field(s) available when an image is uploaded and the correct content is attached to the DS. This method, which was modified from my JIT image formatter utility, in turn allows for dynamic resizing images within a blog post!

edit: to make this even more generic, it can be written like this instead:

<xsl:param name="w">
    <xsl:choose>
        <xsl:when test="$img-val = //post-images/post/entry/image/filename">
            <xsl:value-of select="//post-images/post/entry[image/filename = $img-val]/width" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="450" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:param>

the [@link-id = $entry] was unnecessary. by writing it this way, it will work on a list of blog posts as well as individual ones.

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