 |
| 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
|
|
|
|

January 15th, 2010, 02:29 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
|

January 15th, 2010, 03:00 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You can use the substring-after function.
Code:
<xsl:value-of select="substring-after(@src)" />
|
|

January 15th, 2010, 04:05 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

January 16th, 2010, 01:18 AM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
|

January 16th, 2010, 05:55 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

January 16th, 2010, 06:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|

January 17th, 2010, 11:38 AM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|
 |