Search

Despite using Symphony for a while, I haven’t really had a need to do anything with the ninja technique until now. So I’m trying to take an HTML formatted entry:

<chapter>
    <h2>Title</h2>
    <p>Sentence.</p>
    <p>Sentence.</p>
    <h1>Second title</h1>
    <h2>Second subtitle</h2>
    <p>Sentence</p>
</chapter>    

and append IDs to each element in the output like so:

<h2 id="h2_1">Title</h2>
<p id="p_1">Sentence.</p>
<p id="p_2">Sentence.</p>
<h1 id="h1_1">Second title</h1>
<h2 id="h2_2">Second subtitle</h2>
<p id="p_3">Sentence</p>

I feel like what I want to do should be obvious, but I can’t seem to get it right. No matter what I do, it just numbers things sequentially, here is the closest I’ve been able to get:

<xsl:template match="chapter//*">
    <xsl:element name="{name()}">
        <xsl:attribute name="id">
            <xsl:value-of select="name(.)"/>
            <xsl:text>_</xsl:text>
            <xsl:value-of select="count(preceding-sibling::*[name()]) + 1"/>
        </xsl:attribute>
        <xsl:apply-templates/>
    </xsl:element>  
</xsl:template>

It just counts every preceding node no matter what I put after preceding-sibling:: What am I doing wrong? Am I going down the wrong path?

And the question has been answered!

For posterity if anyone else needs it, the solution was:

count(preceding-sibling::*[name()=name(current())]) + 1

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