Project

Profile

Help

Bug #3787 » regex_test.xslt

Stefan Pöschel, 2018-05-16 08:06

 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:test="http://test.test" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:function name="test:run_regex">
<xsl:param name="value" as="xs:string"/>
<xsl:param name="regex" as="xs:string"/>
<xsl:param name="regex_name" as="xs:string"/>

<regex_result>
--- Testing: <xsl:value-of select="$regex_name"/> ---
Value: '<xsl:value-of select="$value"/>'
Regex: '<xsl:value-of select="$regex"/>'
<xsl:choose>
<xsl:when test="matches($value, $regex)">
-- Match of regex! --
$1: '<xsl:value-of select="replace($value, $regex, '$1')"/>'
$2: '<xsl:value-of select="replace($value, $regex, '$2')"/>'
$3: '<xsl:value-of select="replace($value, $regex, '$3')"/>'
</xsl:when>
<xsl:otherwise>
-- ....fail to match regex! --
</xsl:otherwise>
</xsl:choose>
</regex_result>
</xsl:function>
<xsl:template match="/">
<xsl:variable name="var" select="'x x x x'"/>
<xsl:variable name="regex_single_token1">(\d*\.)?x</xsl:variable>
<xsl:variable name="regex_single_token2">x</xsl:variable>
<result>
<xsl:sequence select="test:run_regex($var, concat('^', $regex_single_token1, '( ', $regex_single_token1, '){2}$'), 'Standard case - shall not match (BUT DOES!)')"/>
<xsl:sequence select="test:run_regex($var, concat('^', $regex_single_token1, '( ', $regex_single_token1, '){3}$'), 'Standard case - shall match')"/>
<xsl:sequence select="test:run_regex($var, concat('^', $regex_single_token2, '( ', $regex_single_token2, '){2}$'), 'Different single token - shall not match')"/>
<xsl:sequence select="test:run_regex($var, concat('^', $regex_single_token2, '( ', $regex_single_token2, '){3}$'), 'Different single token - shall match')"/>
<xsl:sequence select="test:run_regex($var, concat('^', $regex_single_token1, ' ', $regex_single_token1, ' ', $regex_single_token1, '$'), 'Repetition unfolded - shall not match')"/>
<xsl:sequence select="test:run_regex($var, concat('^', $regex_single_token1, ' ', $regex_single_token1, ' ', $regex_single_token1, ' ', $regex_single_token1, '$'), 'Repetition unfolded - shall match')"/>
</result>
</xsl:template>
</xsl:stylesheet>
    (1-1/1)