# How can I give each section a unique <a> tag when I Export as XHTML?

Exporting a notebook as XHTML allows me to override the default HTML tags that are written for section-header-like cells. For example, a notebook cell whose type is "Section", containing text "The Larch" would, by default, be written as the following XHTML:

<p class="Section">The Larch</p>


Now, a section heading might logically be considered to deserve an h4 tag in the output XHTML document (it is alt-4, after all), so I could Export using ConversionRules:

Export["mywebpage.xhtml", myNotebook, "XHTMLMathML",
"ConversionRules" -> { "Section" -> {"<h4 class=\"Section\">", "</h4>"} } ]


and then the output XHTML would read

<h4 class="Section">The Larch</h4>


(Aside: I left the class in the h4 tag just in case. It allows some CSS flexibility, and I also might have different header types that are logically the same kind of h-tag. For example, a "Title" and a "Subtitle" are both logically h1's because they both label the entire document.)

In order to eventually make a nice clickable table of contents, I would like each header-type cell's XHTML to also include an <a id="_something-unique_"> tag, so that the document's XHTML might read:

<h2 class="Chapter"><a id="header-cell-000016">Trees</a></h2>


I tried including a counter in the ConversionRules, something like ToString[headerCounter++], but it was only evaluated once per rule, so all Chapters were 000002 and all Sections were 000004, and so on.

What should I do to make all these <a> tags have unique ids? Is there some combination of Holds and Evaluate or something that will work?

I know I could do it by postprocessing the XHTML, perhaps with XSL or just with Mathematica, but I'm hoping for a more elegant solution.

• The online documentation pages do have it so there must be a way. – Szabolcs Apr 14 '15 at 20:14
• How about using "Section" :> instead? – Taiki Apr 14 '15 at 21:15

Taiki's suggestion works. The "ConversionRules" is now:
"ConversionRules" ->