XSLT 1.0 中的“正则表达式"样式替换 [英] "Regular expression"-style replace in XSLT 1.0

查看:26
本文介绍了XSLT 1.0 中的“正则表达式"样式替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用非常适合正则表达式的 XSLT 1.0 执行查找和替换.不幸的是,这些在 1.0 中不可用,而且由于我无法更改的安全设置,我也无法使用任何扩展库,例如 EXSLT.

I need to perform a find and replace using XSLT 1.0 which is really suited to regular expressions. Unfortunately these aren't available in 1.0 and I'm also unable to use any extension libraries such as EXSLT due to security settings I can't change.

我正在处理的字符串如下所示:

The string I'm working with looks like:

19;#John Smith;#17;#Ben Reynolds;#1;#Terry Jackson

我需要替换数字和;# 字符带有 ,.例如,上面将更改为:

I need to replace the numbers and ; # characters with a ,. For example the above would change to:

约翰·史密斯、本·雷诺兹、特里·杰克逊

我知道需要一个递归字符串函数,可能使用子字符串和翻译,但我不确定从哪里开始.

I know a recursive string function is required, probably using substring and translate, but I'm not sure where to start with it.

有没有人有关于如何解决这个问题的一些指示?这是我的开始:

Does anyone have some pointers on how to work this out? Here's what I've started with:

<xsl:template name="TrimMulti">
    <xsl:param name="FullString" />
    <xsl:variable name="NormalizedString">
        <xsl:value-of select="normalize-space($FullString)" />
    </xsl:variable>
    <xsl:variable name="Hash">#</xsl:variable>
    <xsl:choose>
        <xsl:when test="contains($NormalizedString, $Hash)">
            <!-- Do something and call TrimMulti -->
        </xsl:when>
    </xsl:choose>
</xsl:template>

推荐答案

我希望你没有因为在 SO 上提问而把问题简化太多,因为这应该不是什么大问题.

I'm hoping you haven't simplified the problem too much for asking it on SO, because this shouldn't be that much of a problem.

>

只要保持输入字符串的格式一致,您就可以定义一个模板并递归调用它.

You can define a template and recursively call it as long as you keep the input string's format consistent.

例如

<xsl:template name="TrimMulti">
  <xsl:param name="InputString"/>
  <xsl:variable name="RemainingString" 
    select="substring-after($InputString,';#')"/>
  <xsl:choose>
    <xsl:when test="contains($RemainingString,';#')">
      <xsl:value-of 
        select="substring-before($RemainingString,';#')"/>
      <xsl:text>, </xsl:text>
      <xsl:call-template name="TrimMulti">
        <xsl:with-param 
          name="InputString"
          select="substring-after($RemainingString,';#')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$RemainingString"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

我通过以下调用测试了这个模板:

I tested this template out with the following call:

<xsl:template match="/">
  <xsl:call-template name="TrimMulti">
    <xsl:with-param name="InputString">19;#John Smith;#17;#Ben Reynolds;#1;#Terry Jackson</xsl:with-param>
  </xsl:call-template>
</xsl:template>

并得到以下输出:

John Smith, Ben Reynolds, Terry Jackson

这似乎是您所追求的.

如果您熟悉函数式编程,那么对它的作用的解释很容易解释.InputString 参数总是采用 [number];#[name];#[rest of string] 的形式.TrimMulti 模板的每次调用都会切掉 [number];# 部分并打印掉 [name] 部分,然后传递剩余的表达式递归到自身.

The explanation of what it is doing is easy to explain if you're familiar with functional programming. The InputString parameter is always in the form [number];#[name];#[rest of string]. Each call of the TrimMulti template chops off the [number];# part and prints off the [name] part, then passes the remaining expression to itself recursively.

基本情况是 InputString 的形式为 [number];#[name],在这种情况下,RemainingString 变量获胜'不包含 ;#.由于我们知道这是输入的结尾,所以这次我们不输出逗号.

The base case is when InputString is in the form [number];#[name], in which case the RemainingString variable won't contain ;#. Since we know this is the end of the input, we don't output a comma this time.

这篇关于XSLT 1.0 中的“正则表达式"样式替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆