Subhamoy S.
Subhamoy S.

Reputation: 6764

How to easily generate unique strings in XSLT?

I have a question!

I have an XML document that has sections and subsections. I am generating a Doxygen page out of it using XSLTProc and now I have a problem. When I generate a section name like this:

<xsl:template match="SECTION/SUBSECTION">
@subsection <xsl:value-of select="@title"/>
<xsl:apply-templates/>
</xsl:template>

Then the first word of the title does not show up, because Doxygen expects the declaration in this way:

@subsection <subsectionname> <subsectiontitle>

So, the first word is automatically treated as the subsection name. Putting a randomly generated string there does not seem like a very simple task. I tried to put unique number instead, by using <xsl:value-of select="count(preceding-sibling::*[@col]) + 1", which worked as expected, but as it turns out, Doxygen does not accept numbers as subsection names. I also tried to strip white spaces of "@title" and use that as the subsection name, but XSLTProc complains that it was not an immediate child of <xslt:stylesheet>. How can I easily put some unique string there? It does not have to be meaningful text.

Thanks in advance!

Upvotes: 2

Views: 992

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Use the generate-id() function.

<xsl:value-of select="generate-id(@title)"/> 

If you want the generated string to be more "readable", here is one way to do this:

<xsl:value-of select="concat(@title, generate-id(@title))"/> 

Upvotes: 5

Related Questions