Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2010, 02:29 PM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Remove part of image src path

I have lot of html tags in my xml (well formated) which i need to transform to html using xsl. I am using xsl-copy-of function to copy and display it as is.Everything is working fine, but now i got a problem. I want to remove a string "/mydomain.com" from all image src tags.

ie. <img src="/mydomain.com/Global/Images/test.jpg" />

This should become

<img src="/Global/Images/test.jpg" />

for all img src tags /mydomain.com should be replaced.

I googled and read like using xsl-copy-of we cannot do the replacement. I need to use identity template or so.I dont have much idea, i am not an xsl expert, so can anyone solve the problem for me?


My XML
__________

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<content>
<h1>Hai</h1>
<br />
<img src="/mydomain.com/Global/Images/test.jpg" />
</content>
</root>

test.xsl
__________

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<div>
<xsl:copy-of select="root/content"/>
</div>
</xsl:template>
</xsl:stylesheet>

Last edited by saraths007; January 15th, 2010 at 02:31 PM..
 
Old January 15th, 2010, 03:00 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can use the substring-after function.
Code:
<xsl:value-of select="substring-after(@src)" />
__________________
Joe
http://joe.fawcett.name/
 
Old January 15th, 2010, 04:05 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need two template rules. One that processes the img/@src attributes:

<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:value-of select="substring-after(@src)"/>
</xsl:attribute>
</xsl:template>

and the other processes everything else:

<xsl:template match="*|@node()">
<xsl:copy>
<xsl:apply-templates select="*|@node()"/>
</xsl:copy>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 16th, 2010, 01:18 AM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

I tried to make changes as you suggested, but not seem to be working, can you please check what i am doing wrong?

I want to copy all HTML contents only under root/content and transform it through XSL to produce HTML code.

My XML
__________

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<content>
<h1>Hai</h1>
<br />
<img src="/mydomain.com/Global/Images/test.jpg" />
</content>
</root>

test.xsl
__________

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<div>
<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:value-of select="substring-after(@src,'mydomain.com')"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="*|@node()">
<xsl:copy>
<xsl:apply-templates select="*|@node()"/>
</xsl:copy>
</xsl:template>
</div>
</xsl:template>
</xsl:stylesheet>

Last edited by saraths007; January 16th, 2010 at 01:21 AM..
 
Old January 16th, 2010, 05:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, it should be substring-after(., 'mydomain.com')
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 16th, 2010, 06:02 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Change your code like the one below:

Code:
<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:value-of select="substring-after(., 'mydomain.com')"/>
</xsl:attribute>
</xsl:template>
 
<xsl:template match="*|@node()">
<xsl:copy>
<xsl:apply-templates select="*  | @node()"/>
</xsl:copy>
</xsl:template>
__________________
Rummy
 
Old January 17th, 2010, 11:38 AM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried to implement the above code , it worked fine in some cases

But when ever image tag had some other attributes before src its not working

eg: <img height="100" align="left" width="10" src="/ifc.org/en_US/Global/Images/new.jpg" />


This is not working

<img src="/ifc.org/en_US/Global/Images/new.jpg" /> this is working!!!

Any one can help?

This is what i have used


<xsl:template match="*|@node()">
<xsl:copy>
<xsl:apply-templates select="*|@node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:value-of select="substring-after(.,'mydomain.com')"/>
</xsl:attribute>
</xsl:template>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove part of database field? jenbuh ASP.NET 3.5 Professionals 4 August 10th, 2009 02:36 PM
Could not find a part of the path '../../Web bex ASP.NET 2.0 Basics 4 March 21st, 2009 04:13 PM
Could not find a part of the path babarrana ASP.NET 2.0 Professional 3 August 21st, 2007 09:05 AM
Error: Could not find a part of the path eelisMX Pro VB.NET 2002/2003 2 July 4th, 2006 02:22 AM
sending the src for an image to a new window... elladi Javascript How-To 1 November 29th, 2004 05:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.