|
|
 |
|
Title: search and replace
Submitter: christoph weingarten
(other recipes)
Last Updated: 2001/08/27
Version no: 1.0
Category:
XPath Tricks
|
|
|
Description:
there is no xpath-function like this: replace(needle, haystack).
here's how i managed this functionality. this code will replace ".html" through ".php" in a path.
Source: Text Source
<xsl:choose>
<xsl:when test="substring-before(a/@href, '.html') != '' ">
<xsl:attribute name="href"><xsl:value-of select="substring-before(a/@href, '.html')"/>.php<xsl:value-of select="substring-after(a/@href, '.html')"/></xsl:attribute>
</xsl:when>
</xsl:choose>
The license for this recipe is available here.
Discussion:
you can use this code for example in a framework with diffrent outputdata.
|
|
Add comment
|
|
Number of comments: 2
Replacing element strings, Tim Watts, 2001/08/30
This is a simular recipe using a node from an xml document called text to replace a string within it.
<xsl:choose>
<!-- If 'xyz' is in the text node -->
<xsl:when test="contains(text, 'xyz')">
<!-- then replace it with 'abc' and paste the rest of the element value -->
<xsl:element name="text">
<xsl:value-of select="substring-before(text, 'xyz')"/>
abc
<xsl:value-of select="substring-after(text, 'xyz')"/>
</xsl:element>
</xsl:when>
</xsl:choose>
Add comment
str:subst, Steve Ball, 2001/11/15
The XSLT Standard Library has a general-purpose replace template:
str:subst. Example:
<xsl:variable name="newfilename">
<xsl:call-template name="str:subst">
<xsl:with-param name="text" select="@href"/>
<xsl:with-param name="replace" select="'.php'"/>
<xsl:with-param name='with'>.html
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$newfilename"/>
There are lots of other useful string processing templates in XSLTSL.
In order to use the library, you must setup the "str" XML namespace
in your stylesheet (you can use any prefix you like) and import
or include the stylesheet module. Eg:
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:str='http://xsltsl.org/string"
exclude-result-prefixes='str'>
<xsl:import href='http://xsltsl.sf.net/string.xsl'/>
...
</xsl:stylesheet>
Add comment
|
|
|
|
|
 |
|