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 February 16th, 2011, 05:58 PM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Smile how to change the src of an image in the img tag.

Hi folks,

I am new to xslt ..i am facing problem in changing site adress with in all the image tags present in the xml document..all my images are refering to cup.com which is not available. So, how to change it to dip.com..
How to change the src value of the images from cup.com to dip.com..



<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->

<content>
<m_content>
<title>Title1</title>

<n_content> testing the n content 1

<img src="http://cup.com/images/image1.jpg"/>

</n_content>

</m_content>
<m_content>
<title>title2</title>

<n_content> testing the n content 2

<img src="http://cup.com/images/image2.jpg"/>

</n_content>

</m_content>


</content>


this is the way i wrote the xslt which looks for cup.com which is not available..so i want to change that to dipp to make the images available for the webpage.


<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Content</h2>
<table WIDTH="100%" border="1">
<xsl:for-each select="/content/m_content">
<tr><td>
<xsl:value-of select="title"/>
</td></tr>
<tr><td>

<xsl:value-of select="n_content"/>
</td></tr>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>


</xsl:stylesheet>



all need is how to change all the src attribute in the img elements to refer to dip.com ...thanks in advance..
 
Old February 17th, 2011, 05:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I would write some templates to handle the two cases:

Code:
<xsl:template match="img[contains(@src,'http://cup.com')]">
  <img src="{concat('http://dip.com',substring-after(@src,'http://cup.com'))}"/>
</xsl:template>
	
<xsl:template match="img"><xsl:copy-of select="."/></xsl:template>
Then change the line with n_content to use xsl:apply-templates instead of xsl:value-of:

Code:
<xsl:apply-templates select="n_content"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 17th, 2011, 10:15 AM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Thanks a lot samjudson it worked like charm..

I appreciate ur help..
 
Old February 21st, 2011, 11:15 AM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default This is not working when the img tag is with in the <P> or any other tags like ol/li

Hi,

I tried to implement this logic but it works when ever the <img> tag is with in the n_content .but if the img tag is with in<p> tag like

eg: <n_content><p><img src="/sop.com/"></p></n_content>

its not able to change the src of the img tag. One more problem with this is I have to use <copy-of select="."> for n_content, because the test i entered is not getting fomatted to bold/italic/p/ if i just use <apply-template>. Can anyone help me about this.


<content>
<m_content>
<title>Title1</title>

<n_content> testing the n content 1

<li><img src="http://cup.com/images/image1.jpg"/></li>

</n_content>

</m_content>
<m_content>
<title>title2</title>

<n_content> testing the n content 2

<P><img src="http://cup.com/images/image2.jpg"/></p>

</n_content>

</m_content>


</content>



XSLT:



<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Content</h2>
<table WIDTH="100%" border="1">
<xsl:for-each select="/content/m_content">
<tr><td>
<xsl:value-of select="title"/>
</td></tr>
<tr><td>

<xsl:apply-templates select="n_content"/>

</td></tr>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>
<xsl:template match="img[contains(@src,'http://cup.com')]">
<img src="{concat('http://dip.com',substring-after(@src,'http://cup.com'))}"/>
</xsl:template>

<xsl:template match="img"><xsl:copy-of select="."/></xsl:template>

<xsl:template match="n_content">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
 
Old February 21st, 2011, 11:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

We can only provide you solutions for problems you tell us about.

If you add in an identity template it will cope with all other elements.

Code:
<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 21st, 2011, 12:47 PM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks

I apologize for not posting the complete problem. Thanks for your great help. It's working now.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Refreshing a .img Image wagham ASP.NET 2.0 Basics 9 May 20th, 2009 05:26 PM
<img Tag problem> sen321x .NET Framework 2.0 1 October 30th, 2007 07:47 AM
img tag not loading austinf XSLT 0 June 13th, 2006 04:41 AM
encoding JavaScript in src at script tag jimusa Javascript 1 October 25th, 2005 10:04 AM
img src an aspx location?? texasraven ASP.NET 1.0 and 1.1 Professional 10 May 10th, 2004 04:05 PM





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