XSLT多次替换

时间:2010-01-22 13:11:05

标签: xml xslt replace

我有一个XML文档,比如

<root>
  <item>_x0034_SOME TEXT</item>
  <item>SOME_x0020_TEXT</item>
  <item>SOME_x0020_TEXT_x0032_</item>
</root>

我将其导出为HTML,但是我在替换转义字符时遇到了问题。 我在网上找到了几个模板来进行文本替换,但它们都与此类似:

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

我不知道如何使用它来进行多次替换。我试过这个:

<xsl:for-each select="PinnacleSys.PMC.Plugins.PVR.PvrChannelDescriptorWrapper/PinnacleSys.PMC.Plugins.PVR.DVBTPvrChannelDescriptor">

    <!--name="<xsl:value-of select="replace(replace(Name, '_x0020_', ' '), '_x0034_', '3')"/>" -->
    <!--name="<xsl:value-of select="Name"/>"-->
    <xsl:variable name="var1" select="Text" />
        <xsl:value-of select="replace($FeatureInfo,'Feature=','TESTING')"/>

    name="
        <xsl:call-template name="replaceString">
            <xsl:with-param name="strOrig" select="Name"/>
            <xsl:with-param name="strSearch" select="'_x0020_'"/>
            <xsl:with-param name="strReplace" select="' '"/>
        </xsl:call-template>
        <xsl:call-template name="replaceString">
            <xsl:with-param name="strOrig" select="Name"/>
            <xsl:with-param name="strSearch" select="'_x0030_'"/>
            <xsl:with-param name="strReplace" select="'0'"/>
        </xsl:call-template>
       ..."

但是这只是连接几次字符串,每个都有不同的替换。 我也研究了变量;如果我可以将模板调用的结果分配给变量,我可以得到一个肮脏但有效的解决方案,这对我来说已经足够了。但是我无法也不知道是否可能。

最好的方法是什么?

我只限于1.0 XSLT(使用2.0我可以在另一个内部调用一个replace())。

4 个答案:

答案 0 :(得分:5)

对于使用模板进行替换的本机XSLT 1.0解决方案,需要嵌套单独的替换,如此处所示。由于潜在的替换次数,这显然效率不高。下面给出了优化版本。

<xsl:template match="item">
<xsl:copy>
    <xsl:call-template name="replace-substring">
        <xsl:with-param name="original">
            <xsl:call-template name="replace-substring">
                <xsl:with-param name="original">
                    <xsl:call-template name="replace-substring">
                        <xsl:with-param name="original" select="."/>
                        <xsl:with-param name="substring" select="'_x0020_'"/>
                        <xsl:with-param name="replacement" select="' '"/>
                    </xsl:call-template>
                </xsl:with-param>
                <xsl:with-param name="substring" select="'_x0032_'"/>
                <xsl:with-param name="replacement" select="'2'"/>
            </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="substring" select="'_x0034_'"/>
        <xsl:with-param name="replacement" select="'4'"/>
    </xsl:call-template>
</xsl:copy>
</xsl:template>

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

由于输出是HTML(XML也可以),_xNNNN_字符串可以转换为十六进制数字字符引用,例如&#xNNNN;。这个模板很有效。

<xsl:template match="item">
<xsl:copy>
    <xsl:call-template name="replaceChars">
        <xsl:with-param name="original" select="."/>
    </xsl:call-template>
</xsl:copy>
</xsl:template>

<xsl:template name="replaceChars">
<xsl:param name="original"/>
<xsl:choose>
    <xsl:when test="contains($original, '_x')">
        <xsl:value-of select="substring-before($original, '_x')"/>
        <xsl:variable name="after" select="substring-after($original, '_x')"/>
        <xsl:variable name="char" select="substring-before($after, '_')"/>
        <xsl:value-of select="concat('&amp;#x',$char,';')" disable-output-escaping="yes"/>
        <xsl:call-template name="replaceChars">
            <xsl:with-param name="original" select="substring-after($after, '_')"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$original"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

这是结果输出。

<root>
    <item>&#x0034;SOME TEXT</item>
    <item>SOME&#x0020;TEXT</item>
    <item>SOME&#x0020;TEXT&#x0032;</item>
</root>

答案 1 :(得分:1)

XSLT 1.0不是替换字符串的最佳选择。您拥有的模板将是使用的模板。但是,对于字符串替换,我在另一种语言中使用字符串替换,通常是JavaScript或C#/ VB.NET(Java或任何其他语言也可以)。据推测,在某些时候,XML文档被序列化为字符串。一旦它是一个字符串,使用简单的字符串到字符串替换或正则表达式来进行更复杂的模式匹配。

答案 2 :(得分:1)

将模板调用放在变量体内。

<xsl:variable name="myVar"><xsl:call-template name="replaceString">
        <xsl:with-param name="strOrig" select="Name"/>
        <xsl:with-param name="strSearch" select="'_x0020_'"/>
        <xsl:with-param name="strReplace" select="' '"/>
    </xsl:call-template>
</xsl:variable>

这分配给myVar调用内容。

答案 3 :(得分:1)

在EXSLT(www.exslt.org)中查看正则表达式支持。大多数现代XSL处理器都支持EXSLT。