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

Subject: Re: [xsl] How to substitute a portion of the text value of an element
From: Paul <pflists@xxxxxxxxx>
Date: Tue, 7 Oct 2008 16:03:11 -0400
Another wrinkle... :)

I just noticed that in the same xml document I have an attribute like this:
<box:base-file-name>C:/test10/user/server/logs/server.log</box:base-file-name>

and one like this:

<box:stdout>C:\test10\user\server\logs\server.log</box:stdout>

with both forward and back slashes.

I need to change that c:\test10 directory whether it is c:\test10 or
c:/test10 and ideally make all the slashes in the value of the
elements be the same throughout the xml document. Is there a function
I can call to first convert the slashes to one unified format before
applying the substring?

Thanks,
Paul



On Tue, Oct 7, 2008 at 3:47 PM, Paul <pflists@xxxxxxxxx> wrote:
> Ken,
>
> Thanks! I took your substring code and was able to use it
> successfully. Thanks again!
>
> Paul
>
>
> On Tue, Oct 7, 2008 at 2:53 PM, G. Ken Holman
> <gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
>> 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@xxxxxxxxxxxxxxxxxxxx
>> 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

Current Thread