[Top] [All Lists]

Re: [xsl] XSLT Regex for Matching Curly Braces

2019-06-10 11:56:34
If you want to use xsl:analyze-string with XSLT 2.0 (or 3.0), you can put the regex in a variable, like so:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

  <xsl:variable name="regex" as="xs:string" select="'\{([^}]+)\}'"/>

  <xsl:template match="input">
      <xsl:analyze-string select="." regex="{$regex}">
            <xsl:value-of select="regex-group(1)"/>
          <xsl:value-of select="."/>


(Assuming the input string is wrapped in an <input> element, and replacing Martin's '\p{L)+' with '[^}]+' that matches anything that is not a closing brace, just for variation and for illustrating additional brace/regex complexity.)

Or you can put the regex immediately in the regex attribute, but then you need to duplicate each brace because the processor otherwise attempts to evaluate AVTs within:
<xsl:analyze-string select="." regex="\{{([^}}]+)\}}">


On 10.06.2019 18:48, Martin Honnen martin(_dot_)honnen(_at_)gmx(_dot_)de wrote:
On 10.06.2019 18:30, Don Smith dsmith_lockesmith(_at_)yahoo(_dot_)com wrote:

I'm attempting to write a regex in  XSLT on matching curly braces and
then produce the correct markup output. Between my lack of expertise in
regex generally and the special circumstances in XSLT with regard to
AVTs and curly braces I'm having little success.

The requirement is pretty straight-forward. Given a text node such as this:

} dui {elementum}. Potenti ullamcorper vitae tempus condimentum
dignissim facilisis arcu ligula {elementum} {
I want the output markup to be:

} dui<ph>elementum</ph>. Potenti ullamcorper vitae tempus condimentum
dignissim facilisis arcu ligula<ph>elementum</ph>{
So, two parts to my question:

1. What is the correct regex in XSLT to match a set of curly braces with
one or characters between them and no white space?

It all depends on where you put it.

2. Is <xsl:analyze-string> the best choice for processing the string via
the regex and handling the output or is another approach preferred?

In XSLT 3 I think it is a bit easier using the analyze-string function
and pushing the result through templates e.g. do

<xsl:apply-templates select="analyze-string(., '\{(\p{L}+)\}')"

for your text node and then

   <xsl:template match="match"
           <xsl:value-of select="group"/>

to handle the processing.

Gerrit Imsieke
Geschäftsführer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschäftsführer / Managing Directors:
Gerrit Imsieke, Svea Jelonek, Thomas Schmidt
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com

<Prev in Thread] Current Thread [Next in Thread>