我正在使用XSLT 1.0.我的输入信息可能包含这些值
<!--case 1-->
<attribute>123-00</attribute>
<!--case 2-->
<attribute>Abc-01</attribute>
<!--case 3-->
<attribute>--</attribute>
<!--case 4-->
<attribute>Z2-p01</attribute>
Run Code Online (Sandbox Code Playgroud)
我想找出符合条件的字符串:
if string has at least 1 alphabet AND has at least 1 number,
then
do X processing
else
do Y processing
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,对于情况1,2,4我应该能够进行X
处理.对于案例3,我应该能够进行Y
处理.
我的目标是使用正则表达式(在XSLT 1.0中).
对于所有情况,该属性可以采用任何长度的任何值.
我尝试使用match
,但处理器返回错误.我尝试使用translate
函数,但不确定是否使用正确的方法.
我在考虑.
if String matches [a-zA-Z0-9]*
then do X processing
else
do y processing.
Run Code Online (Sandbox Code Playgroud)
如何使用XSLT 1.0语法实现它?
Dim*_*hev 14
这个解决方案真的适用于XSLT 1.0(并且更简单,因为它不需要也不需要使用双翻译方法.):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="vUpper" select=
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="vLower" select=
"'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vAlpha" select="concat($vUpper, $vLower)"/>
<xsl:variable name="vDigits" select=
"'0123456789'"/>
<xsl:template match="attribute">
<xsl:choose>
<xsl:when test=
"string-length() != string-length(translate(.,$vAlpha,''))
and
string-length() != string-length(translate(.,$vDigits,''))">
Processing X
</xsl:when>
<xsl:otherwise>
Processing Y
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的XML片段时 - 制作格式良好的XML文档:
<t>
<!--case 1-->
<attribute>123-00</attribute>
<!--case 2-->
<attribute>Abc-01</attribute>
<!--case 3-->
<attribute>--</attribute>
<!--case 4-->
<attribute>Z2-p01</attribute>
</t>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
Processing Y
Processing X
Processing Y
Processing X
Run Code Online (Sandbox Code Playgroud)
请注意:任何尝试使用这样的真正的XSLT 1.0处理器代码(从该问题的另一个答案中借用)都将失败并显示错误:
<xsl:template match=
"attribute[
translate(.,
translate(.,
concat($upper, $lower),
''),
'')
and
translate(., translate(., $digit, ''), '')]
">
Run Code Online (Sandbox Code Playgroud)
因为在XSLT 1.0中,禁止匹配模式包含变量引用.