XSLT: Regular Expression function does not work?

Posted by Fedor Steeman on Stack Overflow See other posts from Stack Overflow or by Fedor Steeman
Published on 2010-06-08T12:52:06Z Indexed on 2010/06/08 21:32 UTC
Read the original article Hit count: 606

Filed under:
|
|

Ok, this one has been driving me up the wall...

I have a xslt function that is supposed to split out the Zip-code part from a Zip+City string depending on the country. I cannot get it to work! This is what I got so far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:function name="exslt:GetZip" as="xs:string">
    <xsl:param name="zipandcity" as="xs:string"/>
    <xsl:param name="countrycode" as="xs:string"/>
    <xsl:choose>
        <xsl:when test="$countrycode='DK'">
            <xsl:analyze-string select="$zipandcity" regex="(\d{4}) ([A-Za-zÆØÅæøå]{3,24})">
                <xsl:matching-substring>
                    <xsl:value-of select="regex-group(1)"/>
                </xsl:matching-substring>
                <xsl:non-matching-substring>    
                        <xsl:text>fail</xsl:text>
                </xsl:non-matching-substring> 
            </xsl:analyze-string>               
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>error</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:function>

I am running it on a source XML where the following values are passed to the function:

  • zipandcity: "DK-2640 København SV"
  • countrycode: "DK"

...will output 'fail'!

I think there is something I am misunderstanding here...

© Stack Overflow or respective owner

Related posts about regex

Related posts about xslt