For no nested uicontrol
for nested brackets (that would need parsing for balanced brackets vs. no balanced brackets).
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" name="replace" priority="1">
<xsl:param name="pString" select="."/>
<xsl:variable name="vMask" select="translate($pString,
translate($pString,
'[]',
''),
'')"/>
<xsl:choose>
<xsl:when test="contains($vMask,'[]')">
<xsl:call-template name="makeControl">
<xsl:with-param name="pString" select="$pString"/>
<xsl:with-param name="pMask"
select="substring-before($vMask,'[]')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="makeControl">
<xsl:param name="pString"/>
<xsl:param name="pMask"/>
<xsl:choose>
<xsl:when test="$pMask">
<xsl:variable name="vMask" select="substring($pMask,1,1)"/>
<xsl:value-of select="concat(
substring-before(
$pString,
$vMask),
$vMask)"/>
<xsl:call-template name="makeControl">
<xsl:with-param name="pString"
select="substring-after($pString,$vMask)"/>
<xsl:with-param name="pMask" select="substring($pMask,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($pString,'[')"/>
<uicontrol>
<xsl:value-of select="substring-before(
substring-after(
$pString,
'['),
']')"/>
</uicontrol>
<xsl:call-template name="replace">
<xsl:with-param name="pString"
select="substring-after($pString,']')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output:
<section>
<title>Buttons</title>
<orderedlist>
<listitem>
<para>Clicking on <uicontrol>Save</uicontrol> will attempt to save changes, then it navigates to <xref linkend="saved" xrefstyle="select: title"></xref>.</para>
</listitem>
<listitem>
<para>Clicking on <uicontrol>Cancel</uicontrol> navigates to <xref linkend="noSave" xrefstyle="select: title"></xref>.</para>
</listitem>
</orderedlist>
</section>
And with this input:
<text>
This is an opening bracket [ ? [Yes] [No]
This is a closing bracket ] ? [Yes] [No]
</text>
Output:
<text>
This is an opening bracket [ ? <uicontrol>Yes</uicontrol> <uicontrol>No</uicontrol>
This is a closing bracket ] ? <uicontrol>Yes</uicontrol> <uicontrol>No</uicontrol>
</text>
Note: Any text matching \[[^\[\]]*\]
would be wraped into uicontrol
element.