Search

I swear, this latest site has had me going into areas of XSLT I’ve just never had to deal with before… Man is it a flexible language!

OK, so here’s what I have in mind: I need to scan through a node where all my text is and find any images and turn them to links. The text off these links will be, “See Figure 1” and so on while the src will become the href.

If someone can give me a basic example I think I can get it from there. I was looking at the XSLT Ninja Technique and I know I need at least this:

<xsl:template match="information//img">

</xsl:template>

However, I’m not sure where to go from there. Any tips?

Haha, right, so yet again I was over-thinking things. Surprise, surprise.

Solution:

<xsl:template match="information//img">
    <a rel="figure" href="{@src}">See Figure 1</a>
</xsl:template>

I need to do a bit more to make it have the right number each time but that’s hardly going to be an issue.

I need to do a bit more to make it have the right number each time but that’s hardly going to be an issue.

You could use <xsl:number />. You’ll just have to find the correct level attribute value.

What I actually did was a little different. Since each image was added using the Subsection Manager I matched each link to the specific image in the Subsection Manager and got that node’s position, which I used for the number.

It gives me 1, 2, 3 and so on just as I wanted!

I lied, they’re all showing up as “1”. XSL Number, huh?

Right, I can’t get it to work.

This returns 1 for all items:

<xsl:for-each select="/data/understanding-individual-question/entry/images/item[file/filename = $link]">
    <xsl:number value="position()" format="1"/>
</xsl:for-each>

This returns 2 for all items:

<xsl:number value="position()" format="1"/>

This returns NaN for all items:

<xsl:number value="/data/understanding-individual-question/entry/images/item[file/filename = $link][position()]" format="1"/>

The entire XSLT (with all options shown) is this:

<xsl:template match="information//img">
    <xsl:variable name="link" select="substring-after(@src,'uploads/')" />
    <em>(<a rel="figure" href="{@src}">
        <xsl:text>See Figure </xsl:text>
        <xsl:number value="/data/understanding-individual-question/entry/images/item[file/filename = $link][position()]" format="1"/>
        <xsl:value-of select="/data/understanding-individual-question/entry/images/item[file/filename = $link]"/>
        <xsl:for-each select="/data/understanding-individual-question/entry/images/item[file/filename = $link]">
            <xsl:number value="position()" format="1"/>
        </xsl:for-each>
    </a>.)</em>
</xsl:template>

Bonus point is my xpath to the specific node is correct and returns the right node each time.

Sample XML:

<understanding-individual-question>
    <section id="18" handle="questions">Questions</section>
    <entry id="162">
        <images items="3">
            <item id="215">
                <description mode="normal" handle="winter-frozen-period-for-stile-s-pond" word-count="6">Winter frozen period for Stile’s Pond.</description>
                <file size="73 KB" path="/uploads" type="image/jpg">
                    <filename>lakefrozen-1276880623.jpg</filename>
                    <meta creation="2010-06-18T13:03:43-04:00" width="532" height="479" />
                </file>
                <title mode="normal" handle="stiles-pond-frozen" word-count="3">Stile's Pond Frozen</title>
            </item>
        </images>
    </entry>
</understanding-individual-question>

I have no idea how to get this to work… I tried a solution from StackOverflow but that, too returned NaN.

Have you tried <xsl:number level="any" />?

I tried a number of variations but I keep getting NaN. I’m not sure why this isn’t working…

I also posted the question to StackOverflow and got the answer!

XSLT:

<xsl:for-each select="/data/understanding-individual-question/entry/images/item[file/filename = $link]">
    <xsl:number value="count(preceding-sibling::item) +1" format="1"/>
</xsl:for-each>

[Edited: Added solution as presented at StackOverflow.com]

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