You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT从多行字符串中提取首个电话号码

使用XSLT提取多行字符串中的首个电话号码

方案一:XSLT 2.0及以上版本(推荐)

利用XSLT 2.0+支持的正则表达式功能,通过replace()函数直接捕获首个符合格式的电话号码:

<!-- 定义包含目标内容的变量 -->
<xsl:variable name="input-text" select="'This is a line
This is another line - 012 345 678
This is another line - 097 654 321
This is another line'" />

<!-- 提取首个电话号码 -->
<xsl:variable name="first-phone-number" select="replace($input-text, '^.*?(\d{3} \d{3} \d{3}).*$', '$1')" />

<!-- 输出结果 -->
<xsl:value-of select="$first-phone-number" />

代码说明:

  • 正则表达式^.*?(\d{3} \d{3} \d{3}).*$中:
    • ^.*?:以非贪婪模式匹配字符串开头到首个电话号码前的所有内容
    • (\d{3} \d{3} \d{3}):捕获符合“3位数字+空格+3位数字+空格+3位数字”格式的电话号码
    • .*$:匹配电话号码之后的所有内容
  • replace()函数用捕获到的电话号码($1)替换整个字符串,最终得到首个目标号码。

方案二:XSLT 1.0版本(无正则支持)

如果只能使用XSLT 1.0,可通过递归模板逐行处理字符串,筛选首个符合格式的电话号码:

<xsl:variable name="input-text" select="'This is a line
This is another line - 012 345 678
This is another line - 097 654 321
This is another line'" />

<!-- 递归查找首个电话号码的模板 -->
<xsl:template name="extract-first-phone">
  <xsl:param name="remaining-text" />
  <xsl:param name="line-delimiter" select="'&#10;'" /> <!-- 换行符作为行分隔符 -->
  
  <xsl:choose>
    <!-- 还有多行未处理 -->
    <xsl:when test="contains($remaining-text, $line-delimiter)">
      <xsl:variable name="current-line" select="substring-before($remaining-text, $line-delimiter)" />
      
      <xsl:if test="contains($current-line, '- ')">
        <xsl:variable name="phone-candidate" select="substring-after($current-line, '- ')" />
        <!-- 验证候选内容是否符合电话号码格式 -->
        <xsl:if test="string-length($phone-candidate) = 11 
                      and substring($phone-candidate, 4, 1) = ' ' 
                      and substring($phone-candidate, 8, 1) = ' '
                      and translate($phone-candidate, ' ', '') castable as xs:integer">
          <xsl:value-of select="$phone-candidate" />
          <xsl:return /> <!-- 找到目标后终止递归 -->
        </xsl:if>
      </xsl:if>
      
      <!-- 递归处理剩余行 -->
      <xsl:call-template name="extract-first-phone">
        <xsl:with-param name="remaining-text" select="substring-after($remaining-text, $line-delimiter)" />
      </xsl:call-template>
    </xsl:when>
    <!-- 处理最后一行 -->
    <xsl:otherwise>
      <xsl:if test="contains($remaining-text, '- ')">
        <xsl:variable name="phone-candidate" select="substring-after($remaining-text, '- ')" />
        <xsl:if test="string-length($phone-candidate) = 11 
                      and substring($phone-candidate, 4, 1) = ' ' 
                      and substring($phone-candidate, 8, 1) = ' '
                      and translate($phone-candidate, ' ', '') castable as xs:integer">
          <xsl:value-of select="$phone-candidate" />
        </xsl:if>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- 调用模板提取号码 -->
<xsl:call-template name="extract-first-phone">
  <xsl:with-param name="remaining-text" select="$input-text" />
</xsl:call-template>

代码说明:

  1. 以换行符为分隔符,逐行拆分输入字符串
  2. 检查每行是否包含- ,提取其后的内容作为候选号码
  3. 验证候选内容的长度、空格位置及是否全为数字,符合条件则输出并终止递归

内容的提问来源于stack exchange,提问作者Jjjjjjjjjj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:13:21