|
[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
At 2008-10-07 14:28 -0400, Paul wrote:
I've got an element like this: 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








