user6614827
user6614827

Reputation: 1

Is there a way to replace only part of the value within a node using XSLT

I am attempting to replace only a portion of the value in a node. I've read a few of the articles here and they seem to replace the entire string.

Here's what I have;

<commands>
  <command>chmod 550 OLDPATH/system</command>
  <command>chmod 750 OLDPATH/system/config/home</command>
</commands>  

Here's what I want;

<commands>
 <command>chmod 550 NEWPATH/system</command>
 <command>chmod 750 NEWPATH/system/config/home</command>
</commands>  

Upvotes: 0

Views: 37

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117083

Technically, you can't replace a part of anything in XSLT. However, when you are replacing the entire value of a node, you can use string functions to utilize parts of the value being replaced when building the new value.

For example, applying the following template to your example:

<xsl:template match="command">
    <xsl:copy>
        <xsl:value-of select="substring-before(., 'OLDPATH')" />
        <xsl:text>NEWPATH</xsl:text>
        <xsl:value-of select="substring-after(., 'OLDPATH')" />
    </xsl:copy>
</xsl:template>

would result in:

<command>chmod 550 NEWPATH/system</command>
<command>chmod 750 NEWPATH/system/config/home</command>

Upvotes: 3

Related Questions