Reputation: 2265
Our old code uses v1.0 xsl. I just changed it from :
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
to:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
I am trying to check if any of illegal characters are present in the string using xsl.
This is how my code is doing it right now:
. . .
<xsl:template match="GEN_Limit">
<xsl:if test="string-length(.) > 4">Limit ('<xsl:value-of select="string(.)"/>') - may have up to 4 characters<br/> </xsl:if>
<xsl:if test="string-length(.) = 0">Limit is mandatory<br/></xsl:if>
<xsl:if test="contains((.),'!') or contains((.),'@') or contains((.),'#') or contains((.),'$') or contains((.),'%') or contains((.),'^') or contains((.),'&') or contains((.),'*') or contains((.),'(') or contains((.),')') or contains((.),'+') or contains((.),'=') or contains((.),'{') or contains((.),'}') or contains((.),'[') or contains((.),']') or contains((.),'|') or contains((.),'\') or contains((.),':') or contains((.),';') or contains((.),'<') or contains((.),'>') or contains((.),'?') or contains((.),'/') or contains((.),',') or contains((.),'.')">Data entered - Limit('<xsl:value-of select="string(.)"/>') may not have special characters <br/>
</xsl:if>
</xsl:template>
<xsl:template match="GEN_AcctStationNumber">
<xsl:if test="string-length(.) > 6">Accounting Station Number('<xsl:value-of select="string(.)"/>') - may have up to 6 characters
<br/> </xsl:if>
<xsl:if test="string-length(.) = 0">Accounting Station Number is mandatory<br/></xsl:if>
<xsl:if test="string(/Funding/FundsAgency) ='GEN' and not(string(/Funding/FundsFormCode) ='PP')">
<xsl:if test="string(number(string(.) ) ) ='NaN'">Accounting Station Number must be numeric <br/> </xsl:if>
</xsl:if>
<xsl:if test="contains((.),'!') or contains((.),'@') or contains((.),'#') or contains((.),'$') or contains((.),'%') or contains((.),'^') or contains((.),'&') or contains((.),'*') or contains((.),'(') or contains((.),')') or contains((.),'+') or contains((.),'=') or contains((.),'{') or contains((.),'}') or contains((.),'[') or contains((.),']') or contains((.),'|') or contains((.),'\') or contains((.),':') or contains((.),';') or contains((.),'<') or contains((.),'>') or contains((.),'?') or contains((.),'/') or contains((.),',') or contains((.),'.')">Data entered - Accounting Station Number('<xsl:value-of select="string(.)"/>') may not have special characters <br/>
</xsl:if>
</xsl:template>
<xsl:template match="GEN_SupAcctClassification">
<xsl:if test="string-length(.) > 6">Supplemental Accounting Classification('<xsl:value-of select="string(.)"/>') - may have up to 6 characters
<br/> </xsl:if>
<xsl:if test="string-length(.) = 0">Supplemental Accounting Classification is mandatory<br/></xsl:if>
<xsl:if test="string(/Funding/FundsAgency) ='GEN' and not(string(/Funding/FundsFormCode) ='PP')">
<xsl:if test="string(number(string(.) ) ) ='NaN'">Supplemental Accounting Classification must be numeric <br/> </xsl:if>
</xsl:if>
<xsl:if test="contains((.),'!') or contains((.),'@') or contains((.),'#') or contains((.),'$') or contains((.),'%') or contains((.),'^') or contains((.),'&') or contains((.),'*') or contains((.),'(') or contains((.),')') or contains((.),'+') or contains((.),'=') or contains((.),'{') or contains((.),'}') or contains((.),'[') or contains((.),']') or contains((.),'|') or contains((.),'\') or contains((.),':') or contains((.),';') or contains((.),'<') or contains((.),'>') or contains((.),'?') or contains((.),'/') or contains((.),',') or contains((.),'.')">Data entered - Accounting Station Number('<xsl:value-of select="string(.)"/>') may not have special characters <br/>
</xsl:if>
</xsl:template>
So you can see that contains
is repeated everywhere, in 40 places to be exact. Is there a better way of doing this? Like using matches
, maybe:
<xsl:variable name="illegalCharacters">! @ # $ % ^ & * ( ) \ + = { } [ | ] \ \ : ; < > ? / , .</xsl:variable>
<xsl:if test="matches((.),$illegalCharacters)"></xsl:if>
Or even declare it at the top of the xml and make it usable everywhere since right now, I have that code repeat on 40 places for every template :/
Any ideas?
Upvotes: 0
Views: 61
Reputation: 2265
I went with matches
<xsl:if test="not(matches(., '^[a-zA-Z0-9]+$'))">
Data entered - Accounting Station Number('<xsl:value-of select="string(.)"/>') may not have special characters
</xsl:if>
Upvotes: 0
Reputation: 1614
I think @version should be either 1.0 or 2.0 or a variation, don't think there is a version 17
as for the <xsl:if/>
tag, I'm guessing it falls in a template that matches a node along the lines of <account-num/>
?
the contains()
function, I don't think those parens are necessary
<xsl:if test="contains((.),'!') or
contains(.,'@') or
contains(.,'#')">
Data entered - Account Number <xsl:value-of select="string(.)"/> may
not have special characters <br/>
</xsl:if>
Upvotes: 1