3.6.3.3. Cleaning up dependency lists
The final template in the Antdoc stylesheet is responsible for
tokenizing a comma-separated list of dependencies, inserting pipe
(|) characters between each dependency:
<xsl:template name="fixDependency">
<xsl:param name="depends"/>
The depends parameter may contain text such as
"a, b, c." The template tokenizes this text, producing
the following output:
|a|b|c|
Since XSLT does not have an equivalent to Java's
StringTokenizer class, recursion is required once
again. The technique is to process the text before the first comma
then recursively process everything after the comma. The following
code assigns everything before the first comma to the
firstToken variable:
<xsl:variable name="firstToken">
<xsl:choose>
<xsl:when test="contains($depends, ',')">
<xsl:value-of
select="normalize-space(substring-before($depends, ','))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($depends)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
If the depends parameter contains a comma, the
substring-before( ) function locates the text
before the comma, and normalize-space( ) trims
whitespace. If no commas are found, there must be only one
dependency.
Next, any text after the first comma is assigned to the
remainingTokens variable. If there are no commas,
the remainingTokens variable will contain an empty
string:
<xsl:variable name="remainingTokens"
select="normalize-space(substring-after($depends, ','))"/>
The template then outputs a pipe character followed by the value of
the first token:
<xsl:text>|</xsl:text>
<xsl:value-of select="$firstToken"/>
Next, if the remainingTokens variable is nonempty,
the fixDependency template is instantiated
recursively. Otherwise, another pipe character is output at the end:
<xsl:choose>
<xsl:when test="$remainingTokens">
<xsl:call-template name="fixDependency">
<xsl:with-param name="depends" select="$remainingTokens"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:text>|</xsl:text>
</xsl:otherwise>
</xsl:choose>