0

I have a rss xml file that looks like below with the tag looping many times. I would like to replace the letter 's' with 'm' in the url inside the tag, So http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_s.jpg becomes http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_m.jpg

<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:creativeCommons="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html" xmlns:flickr="urn:flickr:user" version="2.0">
    <channel>
        <title>CCGalleria Pool</title>
        <link>http://www.flickr.com/groups/ccgalleria/pool/</link>
        <item>
        <title>Hampton Court Palace Gardens</title>
        <link>http://www.flickr.com/photos/dksesh/9593294742/in/pool-1540822@N20</link>
        <description>
        <p>
            <a href="http://www.flickr.com/people/dksesh/">dksesh</a>
            has added a photo to the pool:</p>
            <p>
            <a href="http://www.flickr.com/photos/dksesh/9593294742/" title="Hampton Court Palace Gardens">
          <img src="http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_m.jpg" width="240" height="107" alt="Hampton Court Palace Gardens"/>
            </a>
            </p>
    </description>
    <pubDate>Sun, 25 Aug 2013 10:33:09 -0700</pubDate>
    <dc:date.Taken>2013-08-10T16:49:05-08:00</dc:date.Taken>
    <author flickr:profile="http://www.flickr.com/people/dksesh/">[email protected] (dksesh)</author>
    <guid isPermaLink="false">tag:flickr.com,2004:/grouppool/1540822@N20/photo/9593294742</guid>
    <media:content url="http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_b.jpg" type="image/jpeg" height="456" width="1024"/>
    <media:title>Hampton Court Palace Gardens</media:title>
    <media:thumbnail url="http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_s.jpg" HEIGHT="75" WIDTH="75"/>
    <media:credit ROLE="photographer">dksesh</media:credit>
    <creativeCommons:license>http://creativecommons.org/licenses/by-nd/2.0/deed.en_GB</creativeCommons:license>
    </item>
</channel>
</rss>

I have a code something like below, but didn't work. Can someone help? Thanks very much. Newly changed code below.

<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
             xmlns:media="http://search.yahoo.com/mrss/">
  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:for-each select="rss/channel/item">
      <xsl:variable name="newurl" select="replace(media:thumbnail/@url,'_s.jpg','_m.jpg')"/>
      <img src="{$newurl}" style="margin:5px 5px" />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
1
  • XSLT 1.0 is pretty poor at string handling operations such as this. translate will indiscriminately affect all matches within the string, whereas you are interested only in the last one, seemingly. Traditional replacement is normally done in XSLT with a mix of substring-before and substring-after, to split at a point and then re-glue the pieces. Do a search for that - I would have thought there were a lot of questions on SO re: XSLT string replacement. Commented Sep 3, 2013 at 9:35

1 Answer 1

2

Your stylesheet sample showns the version as "2.0" which is good, because there is a replace function you can use in XSLT 2.0 that will probably do the trick here

<xsl:variable name="newurl" select="replace(media:thumbnail/@url,'_s.jpg','_m.jpg')"/>
<img src="{$newurl}" style="margin:5px 5px" />

Note that the second argument of the replace function can actually be a regular expression, if you wanted more control over what 's' needed to be replaced.

As an aside, do note the correct use of Attribute Value Templates here, signified by the curly braces { }. You use them in outputting the src attribute of the img element, but not within the translate/replace function.

Here is the full XSLT in this case:

<xsl:stylesheet version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
             xmlns:media="http://search.yahoo.com/mrss/">

<xsl:output method="html" />

  <xsl:template match="/">
    <xsl:for-each select="rss/channel/item">
      <xsl:variable name="newurl" select="replace(media:thumbnail/@url,'_s.jpg','_m.jpg')"/>
      <img src="{$newurl}" style="margin:5px 5px" />
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your reply. However, when I tried the code below it says 'an error occurred applying the XSL transform'.
<?xml version='1.0'?> <xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:media="search.yahoo.com/mrss"> <xsl:output method="html" /> <xsl:template match="/"> <xsl:for-each select="rss/channel/item"> <xsl:variable name="newurl" select="replace(media:thumbnail/@url,'_s.jpg','_m.jpg')"/> <img src="{$newurl}" style="margin:5px 5px" /> </xsl:for-each> </xsl:template> </xsl:stylesheet>
I've added a full (working) XSLT sample to my answer, which should hopefully help. The code you show in your comment has spurious semi-colons in, and an incorrecr namespace for xsl.
Hi thanks for your reply again. I put the code below, but unfortunately it is still getting the same error.
<?xml version='1.0'?> <xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:media="search.yahoo.com/mrss"> <xsl:output method="html" /> <xsl:template match="/"> <xsl:for-each select="rss/channel/item"> <xsl:variable name="newurl" select="replace(media:thumbnail/@url,'_s.jpg','_m.jpg')"/> <img src="{$newurl}" style="margin:5px 5px" /> </xsl:for-each> </xsl:template> </xsl:stylesheet>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.