[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] lookaheads in XSLT2 regexes


Subject: [xsl] lookaheads in XSLT2 regexes
From: James Cummings <james+xsl@xxxxxxxxxxxxxxxxx>
Date: Fri, 26 Feb 2010 17:16:30 +0000

Hiya,

Does anyone know if there is a way to do positive or negative
lookaheads in XSLT2 regular expressions?

Basically, inside an xsl:analyze-string I am passing a variable as the
contents of @regex.  In the list of regexes for that get passed to
this I'd like to be able to match, say, foo when not followed by an
't' and also match foo when followed by a 't' but not include that 't'
in the string being matched.

I'm terrible at regular expressions, but I believe in various regex
processors in some languages you are able to do things like
foo(?=t) and foo(?!t) to do this and still have the 't' processed
separately. Similarly with lookbehinds.  Does anything like that exist
in XSLT2 regex?

This is in the context of the analyze-string based function that
Martin Honnen (thanks again!) suggested last week looking something
like the below. Where I'm feeding it the $abbr variable.  (And while
previously was escaping this for regex characters, I'm actually
happier feeding in bits of regex.  But the point being I can't use
regex-group() in any of this because I won't know that is what .)

-James

 <xsl:function name="mf:replace" as="node()*">
        <xsl:param name="str" as="xsd:string"/>
        <xsl:param name="abbr" as="element(abbr)*"/>
        <xsl:choose>
            <xsl:when test="$abbr">
                <xsl:analyze-string select="$str" regex="{$abbr[1]}">
                    <xsl:matching-substring>
                        <xsl:copy-of select="$abbr[1]/../ex:egXML/*"
copy-namespaces="no"/>
                    </xsl:matching-substring>
                    <xsl:non-matching-substring>
                        <xsl:sequence select="mf:replace(.,
$abbr[position() gt 1])"/>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$str"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>


Current Thread