2016-01-13 93 views
2

我有这样的XML,(<p>/text()是不同的)XSLT - 替换字符串多次

<doc> 
    <p> solid 1; thick 2; solid 2;</p> 
    <p> double 2; thick 2; dotted 1;</p> 
    <p> dotted 1; double 2; dotted 2;</p> 
    <p> solid 2; thick 2; dotted 2;</p> 
</doc> 

我需求量的是analize <p>节点并替换文本下列字符串,

solid 1; to solid 2; 
solid 2; to solid 4; 
dotted 1; to dotted 2; 
dotted 2; to dotted 4; 

SO,预期的输出应该是这样的,

<doc> 
    <p> solid 2; thick 2; solid 4;</p> 
    <p> double 2; thick 2; dotted 2;</p> 
    <p> dotted 2; double 2; dotted 4;</p> 
    <p> solid 4; thick 2; dotted 4;</p> 
</doc> 

我写了下面的xslt来做这个t请问,

<xsl:template name='replace-text'> 
     <xsl:param name='text'/> 
     <xsl:param name='replace'/> 
     <xsl:param name='by'/> 
     <xsl:choose> 
      <xsl:when test='contains($text, $replace)'> 
       <xsl:value-of select='substring-before($text, $replace)'/> 
       <xsl:value-of select='$by' disable-output-escaping='yes'/> 
       <xsl:call-template name='replace-text'> 
        <xsl:with-param name='text' select='substring-after($text, $replace)'/> 
        <xsl:with-param name='replace' select='$replace'/> 
        <xsl:with-param name='by' select='$by'/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select='$text'/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

    <xsl:template match="p/text()"> 
     <xsl:call-template name="replace-text"> 
      <xsl:with-param name="text" select="."/> 
      <xsl:with-param name="replace" select="'solid 1'"/> 
      <xsl:with-param name="by" select="'solid 2'"/> 
     </xsl:call-template> 
    </xsl:template> 

但是在这里我只能一次传递一个参数。我努力在这种情况下派系编程中实施一种方法,任何人都可以为我提供一种方法来完成这项任务吗?

+0

使用的,每个功能 –

+0

@sanjay,还有一个更通用的解决方案,不使用可变数量的嵌套'replace()'函数调用。该解决方案允许指定更换的外部映射。请享用! –

回答

1

一种更灵活的解决方案,其不使用嵌套replace()呼叫的N个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="p/text()"> 
    <xsl:for-each select="tokenize(., ';')"> 
     <xsl:analyze-string select="." regex="((solid)|(dotted)) (\d)"> 
     <xsl:matching-substring> 
      <xsl:value-of select= 
       "concat(regex-group(1), ' ', 2*xs:integer(regex-group(4)), ';')"/> 
     </xsl:matching-substring> 
     <xsl:non-matching-substring> 
      <xsl:sequence select="."></xsl:sequence> 
     </xsl:non-matching-substring> 
     </xsl:analyze-string> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

甚至更​​通用的解决方案,其中,所述令牌的替换中指定参数(并且可以在单独的XML文档中指定):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:param name="pMapping"> 
    <mapArgOf name="solid" old="1" new="2"/> 
    <mapArgOf name="solid" old="2" new="4"/> 
    <mapArgOf name="dotted" old="1" new="2"/> 
    <mapArgOf name="dotted" old="2" new="4"/> 
</xsl:param> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="p/text()"> 
    <xsl:for-each select="tokenize(., ';')[.]"> 
     <xsl:variable name="vTokens" select="tokenize(., '\s+')[.]"/> 
     <xsl:variable name="vMatch" 
      select="$pMapping/*[@name eq $vTokens[1] and @old eq $vTokens[2]]"/> 

     <xsl:value-of select= 
     "concat(replace(., $vTokens[2], ($vMatch/@new, $vTokens[2])[1]), ';')"/> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

这两个转变,当所提供的XML文档适用的:

<doc> 
    <p> solid 1; thick 2; solid 2;</p> 
    <p> double 2; thick 2; dotted 1;</p> 
    <p> dotted 1; double 2; dotted 2;</p> 
    <p> solid 2; thick 2; dotted 2;</p> 
</doc> 

产生想要的,正确的结果

<doc> 
    <p> solid 2; thick 2; solid 4;</p> 
    <p> double 2; thick 2; dotted 2;</p> 
    <p> dotted 2; double 2; dotted 4;</p> 
    <p> solid 4; thick 2; dotted 4;</p> 
</doc> 

请注意

  1. 我们可以指定尽可能多的映射(不仅适用于“soli d“和”点缀“)。

  2. 在这里,我们不再认为新值的两倍旧值 - 我们甚至不假设值是一个数字

例如,如果我们要添加替代“厚”,使得1由5所取代,2月8日和3所取代由10所取代,我们只需要改变这样的映射:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:param name="pMapping"> 
    <mapArgOf name="solid" old="1" new="2"/> 
    <mapArgOf name="solid" old="2" new="4"/> 
    <mapArgOf name="dotted" old="1" new="2"/> 
    <mapArgOf name="dotted" old="2" new="4"/> 
    <mapArgOf name="thick" old="1" new="5"/> 
    <mapArgOf name="thick" old="2" new="8"/> 
    <mapArgOf name="thick" old="3" new="10"/> 
</xsl:param> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="p/text()"> 
    <xsl:for-each select="tokenize(., ';')[.]"> 
     <xsl:variable name="vTokens" select="tokenize(., '\s+')[.]"/> 
     <xsl:variable name="vMatch" select= 
     "$pMapping/*[@name eq $vTokens[1] and @old eq $vTokens[2]]"/> 

     <xsl:value-of select= 
     "concat(replace(., $vTokens[2], ($vMatch/@new, $vTokens[2])[1]), ';')"/> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

而现在我们又得到了新的,想要的结果:

<doc> 
    <p> solid 2; thick 5; solid 4;</p> 
    <p> double 2; thick 8; dotted 2;</p> 
    <p> dotted 2; double 2; dotted 4;</p> 
    <p> solid 4; thick 10; dotted 4;</p> 
</doc> 

最后,最通用的多替换解决方案看到这个答案

XSL Multiple search and replace function

0

只需使用replace()函数和for-each循环即可。

 <doc> 
     <xsl:for-each select="p"> 
      <p> 
       <xsl:value-of select=" fn:replace(
              fn:replace(
               fn:replace(
                fn:replace(.,'solid 2','solid 4'), 
               'solid 1','solid 2'), 
              'dotted 2','dotted 4'), 
             'dotted 1','dotted 2')"/> 
      </p> 
     </xsl:for-each> 
    </doc> 

在这种情况下,你必须小心第一次替换“固体2”,然后在“固体1”。如果您首先将“固体1”替换为“固体2”,则由于顺序而将再次替换为“固体4”。最内层的函数将首先应用于字符串。