【问题标题】:XSLT1.0 - Replace string in a XML documentXSLT1.0 - 替换 XML 文档中的字符串
【发布时间】:2016-07-07 13:09:51
【问题描述】:

我有一个 XML 文档,我想使用 XSLT 1.0 替换一些特殊的子字符串。我不能使用替换功能(它仅适用于 XSLT 2.0)。出于这个原因,我找到了一个替代解决方案(模板string-replace-all),我正在尝试使用它......但没有成功。 这是一个 XML 示例:

<parent> 
    <child1>hello world!</child1> 
    <child2>example of text</child2> 
</parent>

我想将“世界”替换为“伙计们”。我有这个 xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns="urn:hl7-org:v2xml" xmlns:hl7="urn:hl7-org:v2xml" exclude-result-prefixes="hl7">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<!--Identity template, copia tutto in uscita -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="$text = '' or $replace = '' or not($replace)" >
            <!-- Prevent this routine from hanging -->
            <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <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="text()" >
    <xsl:variable name="newtext">
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="." />
            <xsl:with-param name="replace" select="world" />
            <xsl:with-param name="by" select="guys" />
        </xsl:call-template>
    </xsl:variable>
</xsl:template>
</xsl:stylesheet>

输出是

<parent>
   <child1/>
   <child2/>
</parent>

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    string-replace-all 的调用结果被设置到从未使用过的变量newtext 中。

    只需从template match="text()" 中删除&lt;xsl:variable name="newtext"&gt;&lt;/xsl:variable&gt;

    另外看看@hr_117 的回答:如果你想用guys 替换字符串world,你必须把它们放到' 中。否则搜索元素world

    例如:

    <xsl:template match="text()" >
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="." />
            <xsl:with-param name="replace" select="'world'" />
            <xsl:with-param name="by" select="'guys'" />
        </xsl:call-template>
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      您需要将模板参数更改为字符串。
      试试:

          <xsl:call-template name="string-replace-all">
              <xsl:with-param name="text" select="." />
              <xsl:with-param name="replace" select="'a'" />
              <xsl:with-param name="by" select="'A'" />
          </xsl:call-template>
      

      不带单引号select="a" 的选择正在寻找元素a

      【讨论】:

      • 感谢您的回复。我添加了 ' 但我仍然得到相同的输出(PS:我改变了一点我的例子,将 'a' 替换为 'world' 和 'A' 替换为 'guys')
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多