正则表达式 – 如何在XSLT 1.0中使用正则表达式?

我使用的是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中,禁止匹配模式包含变量引用.

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...