post icon

XSL Replace String XML

Replace String con XSL si può! Ecco lo snippet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="text"/>
<xsl:template match="/">
    <xsl:variable name="myString" select="'Stringa di partenza...'"/>
    <xsl:variable name="myNewString">
        <xsl:call-template name="SubstringReplace">
            <xsl:with-param name="stringIn" select="$myString"/>
            <xsl:with-param name="substringIn" select="'partenza'"/>
            <xsl:with-param name="substringOut" select="'arrivo'"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat('&#10;input: ',$myString,'&#10;output: ',$myNewString)"/>
</xsl:template>

<!-- replacement -->
<xsl:template name="SubstringReplace">
    <xsl:param name="stringIn"/>
    <xsl:param name="substringIn"/>
    <xsl:param name="substringOut"/>
    <xsl:choose>
        <xsl:when test="contains($stringIn,$substringIn)">
            <xsl:value-of select="concat(substring-before($stringIn,$substringIn),$substringOut)"/>
            <xsl:call-template name="SubstringReplace">    
                <xsl:with-param name="stringIn" select="substring-after($stringIn,$substringIn)"/>
                <xsl:with-param name="substringIn" select="$substringIn"/>
                <xsl:with-param name="substringOut" select="$substringOut"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$stringIn"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

Download XML_XSL_Replace.zip (783,00 bytes)

18 febbraio 2009 @ 15:26

No comments yet.

Leave a comment

Leave a Reply

Are you human? Time limit is exhausted. Please reload CAPTCHA.