xsl-list
[Top] [All Lists]

Re: [xsl] How to substitute a portion of the text value of an element

2008-10-07 14:54:36
At 2008-10-07 14:28 -0400, Paul wrote:
I've got an element like this:

<box:base-file-name>C:/test10/user/server/logs/server.log</box:base-file-name>

I'd like it to look like this:

<box:base-file-name>C:/other103/user/server/logs/server.log</box:base-file-name>

I'm passing in an OLD parameter that contains "c:/test10" and a NEW
parameter that contains "c:/other103" but I'm not sure how to
substitute just the OLD portion for the NEW parameter in the value of
box:base-file-name without nuking the rest of the value which I want
to preserve.

Is it possible to just change a portion of the value?

Absolutely. Below I've given two answers, one using substrings (supported in both XSLT 1.0 and 2.0) and one using replace() (supported only in XSLT 2.0) ... but I recommend that you use substrings and *not* replace() because if the $old string has any regular expression pattern characters then you'll get unexpected results.

Another problem with replace() is that it replaces all strings, not just the first.

So, all things considered, just use substring-before() and substring-after().

I hope this helps.

. . . . . . . . . . Ken

T:\ftemp>type paul.xml
<box:base-file-name xmlns:box="urn:x-box">C:/test10/user/server/logs/server.log</box:base-file-name>

T:\ftemp>type paul.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsd="http://www.w3.org/2001/XMLSchema";
                exclude-result-prefixes="xsd"
                version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <result>
    <xslt20>
      <xsl:apply-templates select="*" mode="x2">
        <xsl:with-param name="old">C:/test10</xsl:with-param>
        <xsl:with-param name="new">C:/other103</xsl:with-param>
      </xsl:apply-templates>
    </xslt20>
    <xslt10>
      <xsl:apply-templates select="*" mode="x1">
        <xsl:with-param name="old">C:/test10</xsl:with-param>
        <xsl:with-param name="new">C:/other103</xsl:with-param>
      </xsl:apply-templates>
    </xslt10>
  </result>
</xsl:template>

<xsl:template match="*" mode="x1">
  <xsl:param name="old"/>
  <xsl:param name="new"/>
  <xsl:copy>
    <xsl:value-of select="substring-before(.,$old)"/>
    <xsl:value-of select="$new"/>
    <xsl:value-of select="substring-after(.,$old)"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="x2">
  <xsl:param name="old"/>
  <xsl:param name="new"/>
  <xsl:copy>
<!--WARNING!!!! Assumes the old string doesn't have regex characters and occurs only once-->
    <xsl:value-of select="replace(.,$old,$new)"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xslt2 paul.xml paul.xsl con
<?xml version="1.0" encoding="UTF-8"?>
<result>
   <xslt20>
<box:base-file-name xmlns:box="urn:x-box">C:/other103/user/server/logs/server.log</box:base-file-name>
   </xslt20>
   <xslt10>
<box:base-file-name xmlns:box="urn:x-box">C:/other103/user/server/logs/server.log</box:base-file-name>
   </xslt10>
</result>
T:\ftemp>



--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--