[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to substitute a portion of the text value of a

Subject: Re: How to substitute a portion of the text value of an element
From: Paul <pflists@xxxxxxxxx>
Date: Tue, 7 Oct 2008 15:47:46 -0400
Re:  How to substitute a portion of the text value of a
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.