代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

如何在XSLT中使用fn:replace(字符串、模式、替换)

  •  7
  • Thorin Oakenshield  · 技术社区  · 14 年前

          fn:replace(string,pattern,replace) 
    

    在XSLT中

    2 回复  |  直到 14 年前
        1
  •  16
  •   polygenelubricants    14 年前

    功能规定如下:

    fn:replace($input, $pattern, $replacement, [$flags])
    
    $input        xs:string?  the string to change
    $pattern      xs:string   regular expression to match the areas to be replaced
    $replacement  xs:string   the replacement string
    $flags        xs:string   flags for multiline mode, case insensitivity, etc
    return value  xs:string
    

    $pattern 是一个 regular expression ,替换字符串也有一些特殊的替换语法。

    以下是一些示例:

    # simple replacement
    replace('query', 'r', 'as')               queasy
    
    # character class
    replace('query', '[ry]', 'l')             quell
    
    # capturing group substitution
    replace('abc123', '([a-z])', '$1x')       axbxcx123
    
    # practical example
    replace('2315551212',                     (231) 555-1212
        '(\d{3})(\d{3})(\d{4})',
        '($1) $2-$3'
    )
    

        2
  •  5
  •   Community CDub    8 年前

    我想你应该这样做:

    <xsl:value-of select="fn:replace(value, 'some-pattern', 'with some text')" />
    

    question on stackoverflow