ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: search and replace
Submitter: christoph weingarten (other recipes)
Last Updated: 2001/08/27
Version no: 1.0
Category: XPath Tricks

 

Not Rated yet


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>
	<!-- lets look whether '.html' is in the href-attribute -->
	<xsl:when test="substring-before(a/@href, '.html') != '' ">	
		<!-- replace it through '.php' and paste the rest of the attributes value -->
		<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



Highest rated recipes:

1. Search and Replace

2. Generating a newline

3. Internationalization ...

4. Restricting processing ...

5. Result Pagination with ...

6. Fetching information ...

7. Getting text children of ...

8. Creating empty elements




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.