Search code examples
xmlxsltxslt-1.0libxslt

XSLT 1.0 transform number surrounded by brackets in string


I am trying to convert placeholders strings that look like:

<param>Hello {0}</param>

to output

Hello <%- {0} ->
<ini>
    <params>
        <param name=hello">Hello {0}</param>
    </params>
</ini>

Any suggestions?


Solution

  • I forgot to post my solution, in case anyone runs into this. I ended up changing the definition of my placeholders to be {%token%}.

    <xsl:template name="tokenReplacer">
        <xsl:param name="strToConvert" />
        <xsl:variable name="after" select="substring-after($strToConvert,'{%')"/>
        <xsl:choose>
            <xsl:when test="contains($after,'%}')">
                <xsl:value-of select="substring-before($strToConvert, '{%')" />
                <xsl:text>&lt;%=</xsl:text>
                <xsl:value-of select="substring-before($after,'%}')"/>
                <xsl:text>%&gt;</xsl:text>
                <xsl:call-template name="tokenReplacer">
                    <xsl:with-param name="strToConvert" select="substring-after($after,'%}')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$strToConvert"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>