我们的旧代码使用v1.0 xsl。我只是改变了:
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
为:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
我正在尝试使用xsl检查字符串中是否存在任何非法字符。
这就是我的代码现在正在做的事情:
. . .
<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>
所以你可以看到contains
在任何地方重复,确切地说是40个地方。有没有更好的方法呢?就像使用matches
一样,也许:
<xsl:variable name="illegalCharacters">! @ # $ % ^ & * ( ) \ + = { } [ | ] \ \ : ; < > ? / , .</xsl:variable>
<xsl:if test="matches((.),$illegalCharacters)"></xsl:if>
或者甚至将它声明在xml的顶部并使其可以在任何地方使用,因为现在,我在每个模板的40个位置重复该代码:/
有什么想法吗?
答案 0 :(得分:1)
我认为@version应该是1.0或2.0或变体,不要认为有版本17
对于<xsl:if/>
标记,我猜测它属于与<account-num/>
行的节点匹配的模板吗?
contains()
功能,我不认为这些必要是
<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>
答案 1 :(得分:0)
我选择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>