Reputation: 47
I need to perform validation on the name field to see if it has any illegal characters. Below is the way ma currently doing it
<xsl:variable name="validChars" select="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. "></xsl:variable>
<wd:First_Name>
<xsl:variable name="illegalFirst" select="replace($getUserData.firstname, $validChars, '')" />
<xsl:value-of select="replace($getUserData.firstname, $illegalFirst, '')"/>
</wd:First_Name>
The first replace() captures any/all illegal characters and the second replace() removes those captured characters.
The problem am facing is, it's not necessary that "Name" will always have some illegal characters. It can be a valid name as well. In this situation, the variable $illegalFirst will be empty. This is throwing an error "An empty sequence is not allowed as the second argument of replace()".
I tried to validate as below
<xsl:if test="string-length($illegalFirst) > 0">
<xsl:value-of select="replace($getUserData.firstname, $illegalFirst, '')"/>
</xsl:if>
<xsl:if test="string-length($illegalFirst) = 0">
<xsl:value-of select="$getUserData.firstname"/>
</xsl:if>
This condition check doesn't seem to work. It still throws the same error message.
Am pulling my hair out since morning. Any help is greatly appreciated.
Thanks, Sam.
Upvotes: 1
Views: 1681
Reputation: 116959
What you are attempting to do is an XSLT 1.0 idiom, and it uses the translate()
function:
<xsl:variable name="validChars" select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '"/>
<xsl:value-of select="translate($input-string, translate($input-string, $validChars, ''), '')"/>
Note the single quotes used when defining the $validChars variable.
In XSLT 2.0, you can dispense with the double filtering, and use simply:
<xsl:value-of select="replace($input-string, '[^a-zA-Z. ]', '')"/>
Upvotes: 3
Reputation: 89285
There are some errors in your XSL snippet, firstly, you need to enclose literal string with quotes here :
<xsl:variable name="validChars" select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '"></xsl:variable>
..otherwise above selector will be interpreted as instruction to select child element having that nonsense name like : <abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.>
, which I believe nowhere near your actual intention.
The second is, you need to realize that replace()
function finds substring within the first parameter that matches exactly the 2nd parameter, and then replace matched substring with the 3rd parameter. Again I strongly suspect this isn't what you intended to do. I think want to use translate()
function instead of replace()
here :
<wd:First_Name>
<xsl:variable name="illegalFirst" select="translate($getUserData.firstname, $validChars, '')" />
<xsl:value-of select="translate($getUserData.firstname, $illegalFirst, '')"/>
</wd:First_Name>
side note : translate()
function worked for me even in the case when there is no illegal character found (though, I tested in XSLT 1.0 only).
Upvotes: 0