Search

This may be crazy, but I am wondering if there’s a way to dynamically name CSS id’s.

For instance, if this is my XSL output:

<copy>
    <section id="10" handle="copy">Copy</section>
    <entry id="13">
        <title handle="espresso">Espresso</title>
        <body"><p>The first machines were introduced in Italy at the beginning of the 20th Century.</p></body>
    </entry>

could I select, say, the entry id, and pass it (somehow using a fancy x-path) into the CSS id so it outputs like so:

<div id="13">
     some content . . .
</div>

Probably not possible, but just wondering.

Of course that’s possible. :-)

<xsl:template match="copy/entry">
    <div id="{@id}">
        ...
    </div>
</xsl:template>

If you need more advanced logic in your id attribute you can also use <xsl:attribute name="id" />.

And you should remember that HTML ids cannot start with a number.

Excellent! Thanks!

@lbradford - if you don’t mind me asking, what are you trying to build?

(slightly off-topic)

@Makenosound that’s not entirely correct: HTML5 allows ID’s to start with a number (and a whole lot more). We’ve been able do it for a long time (browsers handle it just fine) but in HTML5 it is actually valid.

Still, I would not advocate starting with a number because you would have to escape the rule in CSS but it works fine.

@bzerangue - It’s just a super simple navigation/’Contents’ to make it easier to get through a long list of content.

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