Reputation: 575
I have an XML node containing data I want to turn into an unordered list. Like so:
<list><![CDATA[*Lorem ipsumdolor sit *consectetur adipiscing elit, * sed do eiusmod * tempor incididunt ut]]></list>
I'm wanting to separate the list elements on the asterisks.
So the output will be:
<ul>
<li>Lorem ipsumdolor sit</li>
<li>consectetur adipiscing elit,</li>
<li>sed do eiusmod</li>
<li>tempor incididunt ut</li>
</ul>
I first tried using a recursive string replace template applied to the data, but I was having trouble wrapping the end up in an</li>
tag.
Upvotes: 1
Views: 136
Reputation: 3435
try this
<xsl:template match="list">
<ul>
<xsl:call-template name="li">
<xsl:with-param name="listdata" select="string(.)"/>
</xsl:call-template>
</ul>
</xsl:template>
<xsl:template name="li">
<xsl:param name="listdata"/>
<xsl:variable name="lidata">
<xsl:choose>
<xsl:when test="contains($listdata, '*')">
<xsl:value-of select="normalize-space(substring-before($listdata, '*'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($listdata)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="lidataremaindata" select="normalize-space(substring-after($listdata, '*'))"/>
<xsl:if test="normalize-space($lidata) != ''">
<li>
<xsl:value-of select="$lidata"/>
</li>
</xsl:if>
<xsl:if test="normalize-space($lidataremaindata) != ''">
<xsl:call-template name="li">
<xsl:with-param name="listdata" select="$lidataremaindata"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Upvotes: 1