如何在XSLT 1中替换多个文本子串

时间:2012-01-17 22:40:05

标签: xml xslt

使用XSLT 1.0,XSLT 2.0的正则表达式方法通常不可用。是否有任何非正则表达式替换源xml文档中节点中的多个字段,例如转换:

<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
  <file>
    <source>abc [[field1]] def [[field2]] ghi</source>
  </file>
</xliff>

为:

<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
  <file>
    <source>abc F def F ghi</source>
  </file>
</xliff>

4 个答案:

答案 0 :(得分:4)

<强>予。 XSLT 1.0解决方案:

此转化

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

 <xsl:param name="pTargetStart" select="'[['"/>
 <xsl:param name="pTargetEnd" select="']]'"/>
 <xsl:param name="pReplacement" select="'F'"/>

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

 <xsl:template match="source/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pTargetStart" select="$pTargetStart"/>
  <xsl:param name="pTargetEnd" select="$pTargetEnd"/>
  <xsl:param name="pRep" select="$pReplacement"/>

  <xsl:choose>
   <xsl:when test=
    "not(contains($pText, $pTargetStart)
       and
        contains($pText, $pTargetEnd)
        )
     or
      not(contains(substring-after($pText, $pTargetStart),
                   $pTargetEnd
                   )
         )
    ">
     <xsl:value-of select="$pText"/>
    </xsl:when>

    <xsl:otherwise>
     <xsl:value-of select="substring-before($pText, $pTargetStart)"/>
     <xsl:value-of select="$pRep"/>

     <xsl:variable name="vremText" select=
     "substring-after(substring-after($pText, $pTargetStart),
                      $pTargetEnd
                      )"/>
     <xsl:call-template name="replace">
      <xsl:with-param name="pText" select="$vremText"/>
      <xsl:with-param name="pTargetStart" select="$pTargetStart"/>
      <xsl:with-param name="pTargetEnd" select="$pTargetEnd"/>
      <xsl:with-param name="pRep" select="$pRep"/>
     </xsl:call-template>
    </xsl:otherwise>

  </xsl:choose>

 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
    <file>
        <source>abc [[field1]] def [[field2]] ghi</source>
    </file>
</xliff>

生成想要的正确结果

<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
   <file>
      <source>abc F def F ghi</source>
   </file>
</xliff>

<强> II。 XSLT 2.0解决方案(仅用于比较):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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="source/text()">
  <xsl:sequence select="replace(., '\[\[(.*?)\]\]', 'F')"/>
 </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:2)

EXSLT为您提供了一些很好的功能。如果您需要替换简单字符串,请尝试str:replace。给出了一个XSLT 1.0 template implementation

答案 2 :(得分:1)

编辑1

我刚刚意识到Dimitre的版本使用递归并且非常相似;所以我的开场句现在看起来很傻。

这是一个使用递归的版本:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:variable name="fld-beg" select="'[['"/>
  <xsl:variable name="fld-end" select="']]'"/>
  <xsl:variable name="replacement" select="'F'"/>

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

  <xsl:template match="source/text()">
    <xsl:call-template name="replace">
      <xsl:with-param name="str" select="."/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="replace">
    <xsl:param name="str"/>
    <xsl:choose>
      <xsl:when test="contains($str, $fld-beg) and contains($str, $fld-end)">
        <xsl:call-template name="replace">
          <xsl:with-param name="str" select="concat(
            substring-before($str, $fld-beg),
            $replacement,
            substring-after($str, $fld-end))"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$str"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

match="source/text()"将“source”节点中的所有文本与一个字符串匹配,并将其传递给命名模式“replace”。 'replace'查找起始和结束分隔符('[['和']]')的出现,如果发现将文本拆分(从而忽略)分隔符,则插入替换字符串,并将所有内容传递给自身重复这个过程。

我说“拆分”,但鉴于XPath 1.0中缺少真正的split(),我们可以合并substring-before()substring-after()

鉴于源'abc [[field1]] def [[field2]] ghi'中的文本,递归就像这样,显示它是如何拆分,替换和传递的:

  1. 'abc ' + 'F' + def [[field2]] ghi',再次传递给'替换'
  2. 'abc F def ' + 'F' + ' ghi',再次传递给'替换'
  3. 由于分隔符不存在,'abc F def F ghi'会传回match="source/text()"
  4. 以下是xsltproc的外观:

    $ xsltproc so.xsl so.xml
    <?xml version="1.0"?>
    <xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
      <file>
        <source>abc F def F ghi</source>
      </file>
    </xliff>
    

    我希望这会有所帮助。

答案 3 :(得分:1)

您可以在XSL中使用Java,例如replaceAll:

<xsl:template name="replace_all" xmlns:string="java.lang.String">
    <xsl:param name="text"/>
    <xsl:param name="pattern"/>
    <xsl:param name="replace"/>
    <xsl:variable name="text_string" select="string:new($text)"/>
    <xsl:value-of select="string:replaceAll($text_string, $pattern, $replace)"/>
</xsl:template>

模式是一个正则表达式。有关更多信息,请参阅 String javadoc

相关问题