正则表达式 – 如何在XSLT 1.0中使用正则表达式?
发布时间:2020-12-14 06:29:23 所属栏目:百科 来源:网络整理
导读:我使用的是XSLT 1.0. 我的输入信息可以包含这些值 !--case 1--attribute123-00/attribute!--case 2--attributeAbc-01/attribute!--case 3--attribute--/attribute!--case 4--attributeZ2-p01/attribute 我想找出符合条件的字符串: if string has at least 1
我使用的是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> 我想找出符合条件的字符串: if string has at least 1 alphabet AND has at least 1 number,then do X processing else do Y processing 在上面的例子中,对于情况1,2,4,我应该能够进行X处理.对于情况3,我应该可以做Y处理. 我的目标是使用正则表达式(在XSLT 1.0中). 对于所有的情况,属性可以取任何长度的任何值. 我尝试使用匹配,但处理器返回错误. 我在想 if String matches [a-zA-Z0-9]* then do X processing else do y processing. 如何使用XSLT 1.0语法实现?
这个解决方案真的适用于XSLT 1.0(而且更简单,因为它并不需要使用double-translate方法.):
<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> 当应用于提供的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> 想要的,正确的结果是产生的: Processing Y Processing X Processing Y Processing X 注意:任何试图使用真正的XSLT 1.0处理器代码(从这个问题的另一个答案中借用)将失败,并显示错误: <xsl:template match= "attribute[ translate(.,translate(.,concat($upper,$lower),''),'') and translate(.,$digit,'')] "> 因为在XSLT 1.0中,禁止匹配模式包含变量引用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |