RE: [xsl] Translating "("

Subject: RE: [xsl] Translating "("
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 15 Nov 2004 13:03:34 -0000
> I need translate special characters like "(", ")" etc in <ln:term>.
> 

You presumably want to generate element nodes in the result tree, rather
than constructing angle brackets within a text node. To construct an element
node, you need to use either a literal result element or xsl:element - you
can't do it by generating the XML markup directly as a string.

This use case (assuming parens are not nested) becomes very easy in XSLT 2.0
with xsl:analyze-string: something like

<xsl:analyze-string select="." regex="\(.*?\)">
  <xsl:matching-substring>
    <ln:term>
      <xsl:value-of select="translate(., '()', '')"/>
    </ln:term>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>

With 1.0, assuming you can have multiple instances of (..) within a text
node, but not nested instances, it needs a recursive template along the
following lines:

output substring-before "("
<ln:term>
  output substring-before ")" substring-after "("
</ln:term>
recursive call to process substring-after ")"

Michael Kay

Current Thread