XSL字符串多重替换功能 [英] XSL string multi-replace function

查看:114
本文介绍了XSL字符串多重替换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使这个函数做多重替换?

exp:用 111 aaa c>并用 222 ...等替换 bbb

 < xsl:call-template name =replace-string> 
< xsl:with-param name =textselect =sm:changefreq/>
< xsl:with-param name =replaceselect ='aaa'/>
< / xsl:call-template>

< xsl:template name =replace-string>
< xsl:param name =text/>
< xsl:param name =replace/>
< xsl:param name =with/>
< xsl:when test =contains($ text,$ replace)>
< xsl:value-of select =substring-before($ text,$ replace)/>
< xsl:call-template name =replace-string>
< xsl:with-param name =replaceselect =$ replace/>
< xsl:with-param name =withselect =$ with/>
< / xsl:call-template>
< / xsl:when>
< xsl:otherwise>
< xsl:value-of select =$ text/>
< / xsl:otherwise>
< / xsl:template>






@Mads Hansen

i是新手..

是最终的代码将会像代码波纹管?



index.xml

 < ;?xml version =1.0encoding =ISO-8859-1?> 
<?xml-stylesheet type =text / xslhref =index.xsl?>
< t>快速
棕色狐狸< / t>

index.xsl

 <?xml version =1.0encoding =ISO-8859-1?> 
< xsl:stylesheet version =1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform
xmlns:my =my:我的>
< xsl:output omit-xml-declaration =yes/>

< my:params xml:space =preserve>
< pattern>
< old>&#xA;< / old>
< new>< br />< / new>
< / pattern>
< pattern>
<旧>快速< /旧>
< new>慢< / new>
< / pattern>
< pattern>
< old> fox< / old>
<新>大象< / new>
< / pattern>
< pattern>
< old> brown< / old>
< new>白色< / new>
< / pattern>
< / my:params>

select =document('')/ * / my:params / */>

< xsl:template match =text()name =multiReplace>
< xsl:param name =pTextselect =。/>
< xsl:param name =pPatternsselect =$ vPats/>

< xsl:if test =string-length($ pText)> 0>
$ vPats [starts-with($ pText,old)] [1]/>

< xsl:when test =not($ vPat)>
< xsl:copy-of select =substring($ pText,1,1)/>
< / xsl:when>
< xsl:otherwise>
< xsl:copy-of select =$ vPat / new / node()/>
< / xsl:otherwise>

< xsl:call-template name =multiReplace>
< xsl:with-param name =pTextselect =
substring($ pText,1 + not($ vPat)+ string-length($ vPat / old / node())) />
< / xsl:call-template>
< / xsl:if>
< / xsl:template>
< / xsl:stylesheet>


解决方案

这是一个完整的XSLT 1.0解决方案多重替换问题

 < xsl:stylesheet version =1.0
xmlns: xsl =http://www.w3.org/1999/XSL/Transform
xmlns:my =my:my>
< xsl:output omit-xml-declaration =yes/>

< my:params xml:space =preserve>
< pattern>
< old>&#xA;< / old>
< new>< br />< / new>
< / pattern>
< pattern>
<旧>快速< /旧>
< new>慢< / new>
< / pattern>
< pattern>
< old> fox< / old>
<新>大象< / new>
< / pattern>
< pattern>
< old> brown< / old>
< new>白色< / new>
< / pattern>
< / my:params>

select =document('')/ * / my:params / */>

< xsl:template match =text()name =multiReplace>
< xsl:param name =pTextselect =。/>
< xsl:param name =pPatternsselect =$ vPats/>

< xsl:if test =string-length($ pText)> 0>
$ vPats [starts-with($ pText,old)] [1]/>

< xsl:when test =not($ vPat)>
< xsl:copy-of select =substring($ pText,1,1)/>
< / xsl:when>
< xsl:otherwise>
< xsl:copy-of select =$ vPat / new / node()/>
< / xsl:otherwise>

< xsl:call-template name =multiReplace>
< xsl:with-param name =pTextselect =
substring($ pText,1 + not($ vPat)+ string-length($ vPat / old / node())) />
< / xsl:call-template>
< / xsl:if>
< / xsl:template>
< / xsl:stylesheet>

当此转换应用于以下XML文档时:

 < t>快速
棕色狐狸< / t>

想要产生正确的结果(quick - > slow,brown - >白色,狐狸 - >大象,NL - > < br /> ):

 缓慢的< br />白象

/ strong>:


  1. 使用递归调用自己的命名模板。


  2. 所有多个替换模式 - >替换对都在单个外部参数中提供,为了方便起见,这里将内联指定为全局级元素< my:params>


  3. 递归将源字符串中的每个字符(从左到右)找到以此开头的第一个模式字符在字符串中的这个位置。


  4. 替换不仅可以是字符串,也可以是任何节点。在这种特殊情况下,我们用< br /> 元素替换每个NL字符。



how to make this function do multi-replacing ?
exp: replace aaa with 111 and replace bbb with 222 ...etc

<xsl:call-template name="replace-string">
  <xsl:with-param name="text" select="sm:changefreq"/>
  <xsl:with-param name="replace" select="'aaa'" />
  <xsl:with-param name="with" select="'111ً'"/>
</xsl:call-template>

<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>


@Mads Hansen
i am newbie ..
are the final codes will be like the codes bellow ?

index.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<t>The quick
brown fox</t>

index.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <my:params xml:space="preserve">
        <pattern>
            <old>&#xA;</old>
            <new><br/></new>
        </pattern>
        <pattern>
            <old>quick</old>
            <new>slow</new>
        </pattern>
        <pattern>
            <old>fox</old>
            <new>elephant</new>
        </pattern>
        <pattern>
            <old>brown</old>
            <new>white</new>
        </pattern>
    </my:params>

    <xsl:variable name="vPats"
         select="document('')/*/my:params/*"/>

    <xsl:template match="text()" name="multiReplace">
        <xsl:param name="pText" select="."/>
        <xsl:param name="pPatterns" select="$vPats"/>

        <xsl:if test="string-length($pText) >0">
            <xsl:variable name="vPat" select=
            "$vPats[starts-with($pText, old)][1]"/>

            <xsl:choose>
                <xsl:when test="not($vPat)">
                    <xsl:copy-of select="substring($pText,1,1)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$vPat/new/node()"/>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:call-template name="multiReplace">
                <xsl:with-param name="pText" select=
                "substring($pText, 1 + not($vPat) + string-length($vPat/old/node()))"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

解决方案

Here is a complete XSLT 1.0 solution to the multiple-replace problem:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <my:params xml:space="preserve">
        <pattern>
            <old>&#xA;</old>
            <new><br/></new>
        </pattern>
        <pattern>
            <old>quick</old>
            <new>slow</new>
        </pattern>
        <pattern>
            <old>fox</old>
            <new>elephant</new>
        </pattern>
        <pattern>
            <old>brown</old>
            <new>white</new>
        </pattern>
    </my:params>

    <xsl:variable name="vPats"
         select="document('')/*/my:params/*"/>

    <xsl:template match="text()" name="multiReplace">
        <xsl:param name="pText" select="."/>
        <xsl:param name="pPatterns" select="$vPats"/>

        <xsl:if test="string-length($pText) >0">
            <xsl:variable name="vPat" select=
            "$vPats[starts-with($pText, old)][1]"/>

            <xsl:choose>
                <xsl:when test="not($vPat)">
                    <xsl:copy-of select="substring($pText,1,1)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$vPat/new/node()"/>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:call-template name="multiReplace">
                <xsl:with-param name="pText" select=
                "substring($pText, 1 + not($vPat) + string-length($vPat/old/node()))"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>The quick
brown fox</t>

the wanted, correct result is produced (quick -> slow, brown -> white, fox -> elephant, NL -> <br/>):

The slow<br />white elephant

Explanation:

  1. A named template that calls itself recursively is used.

  2. All multiple replacement pattern --> replacement pairs are provided in a single external parameter, which for convenience here is specified inline as the global-level element <my:params> .

  3. The recursion takes every single character in the source string (from left to right) and finds the first pattern that starts with this character at this position in the string.

  4. The replacement can be not only a string but also any node. In this specific case we are replacing every NL character with a <br/> element.

这篇关于XSL字符串多重替换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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