簡體   English   中英

XSLT1.0-替換XML文檔中的字符串

[英]XSLT1.0 - Replace string in a XML document

我有一個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>

調用string-replace-all被設置到從未使用過的變量newtext中。

只需從template match="text()"刪除<xsl:variable name="newtext"></xsl:variable>

並且還要查看@ 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>

您需要將template參數更改為字符串。
嘗試:

    <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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM