Find and replace a string inside an image path - xslt

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>
dksesh
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/">nobody#flickr.com (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>

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>

Related

xsl to get atom field

THIS IS MY xsl after Title I need to display VacancyID from below xml can you any help me how to get this value
unable to access atom-field in xsl
need help to fix this
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>abc</title>
<link>https://google.com</link>
<description>test</description>
<item>
<link>https://google.com</link>
<title>abc</title>
<a10:updated>2016-06-22T10:44:49Z</a10:updated>
<a10:content type="text/xml">
<Vacancy>
<VacancyID>123</VacancyID>
</Vacancy>
</a10:content>
</item>
</channel>
</rss>
xsl:
THIS IS MY xsl after Title i need to display VacancyID from ablove xml can you any help me how to get this valu
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<div id="press-release">
<xsl:apply-templates select="rss/channel/item"/>
</div>
</xsl:template>
<xsl:template match="item">
<div class="press-item">
<a href="{./link}" target="_blank" class="title" title="{./title}">
<xsl:value-of select="./title" disable-output-escaping="yes" />
vacancyID????????????(need heleher)
</a>
<xsl:template match="atom:content">
From within your template matching item, you can use:
<xsl:value-of select="atom:content/Vacancy/VacancyID" />
to get the value of VacancyID.

XSLT RSS FEED - Combine substring-before and substring-after

My apologies in advance if this question is really simple, but I can’t seem to find a way around this issue.
I need a way to combine the substring-before and substring-after function in xsl so I have a start and end point within a description element of an RSS feed.
In each description tag I want to extract everything from ‘Primary Title’ onwards, but stop as soon as it reaches the first <b> tag.
I tried the following xsl without much success
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="channel">
<xsl:for-each select="item">
<xsl:value-of select=substring-after(description, 'Primary Title:' />
<xsl:value-of select=substring-before(description, '&ltb&gt' />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Below is the XML I am currently working with.
<rss version="2.0">
<channel>
<item>
<title>Article_110224_081057</title>
<description>
<![CDATA[<div><b>Description:</b>This is my description<b>Primary Title:</b>This is my primary title<b>Second Title:</b>This is my second title title </div>
]]>
</description>
</item>
<item>
<title>Article_110224_081057</title>
<description>
<![CDATA[<div><b>Description:</b>This is my description<b>Other Title:</b>This is my other title<b>Second Title:</b>This is my second title titleb<b>Primary Title:</b>This is my primary title<b> more text </div>
]]>
</description>
</item>
</channel>
</rss>
If the <b> is a tag, you won't be able to find it using substring matching, because tags get turned into nodes by the parser. You'll only be able to match it as a substring if it isn't a tag, for example because it was contained in a CDATA section (which appears to be the case in your example).
May be this can help:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="channel">
<xsl:for-each select="item">
<xsl:value-of select="
substring-after(
substring-before(
substring-after(description, 'Primary Title:'),
'<b'
),
'b>'
)
"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Result against your sample is:
This is my primary titleThis is my primary title

Looping in XSLT

I have the following XML
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="sample.xsl" type="text/xsl"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel
xmlns:cfi="http://www.microsoft.com/schemas/rss/core/2005/internal">
<title cf:type="text">The Hindu - Front Page</title>
<link>http://www.hindu.com/</link>
<description cf:type="text">The Internet edition of The Hindu,
India's national newspaper</description>
<image>
<url>http://www.hindu.com/hindu/hindux.gif</url>
<title>hindu.com</title>
<link>http://www.hindu.com/</link>
</image>
<item>
<title cf:type="text"
xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
>ISRO spectrum deal under review: Centre</title>
</item>
<item>
<title cf:type="text"
xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
>Response from Devas</title>
</item>
</channel>
</rss>
The rss/channel/item can be of any count(in the current case it's count is 2). I need to display the Titles as a Marquee one after the other as follows
ISRO spectrum deal under review: Centre, Response from Devas,....,....
How can I accomplish this in XSLT? kindly advice
Thanks
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cfi="http://www.microsoft.com/schemas/rss/core/2005/internal"
xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"
xmlns:dc="http://purl.org/dc/elements/1.1/"
exclude-result-prefixes="cfi cf dc">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/*">
<div id="marquee">
<xsl:apply-templates select="channel/item/title"/>
</div>
</xsl:template>
<xsl:template match="title">
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">, </xsl:if>
</xsl:template>
</xsl:stylesheet>
Result against your sample:
<div id="marquee">ISRO spectrum deal under review: Centre, Response from Devas</div>
In addition to #Flack correct answer, in XSLT 2.0 xsl:value-of instruction preserves sequence. So, this stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<div id="marquee">
<xsl:value-of select="rss/channel/item/title"
separator=", "/>
</div>
</xsl:template>
</xsl:stylesheet>
Also outputs:
<div id="marquee"
>ISRO spectrum deal under review: Centre, Response from Devas</div>

XSL namespaces and xsl:for-each

I'm currently stuck trying to use XSL to transform a XML document into HTML. The XML document uses namespaces and I don't really have too much experience with XSL let alone namespaces. Basically all I want to do is grab every instance of s:treatment and output this as a list. I've changed data so not to expose the website I'm doing this for. I'm using Classic ASP (can't update to ASP.NET) to transform the XML on the server, so the XSL has to be version 1 :(
Any help would be really appreciated here as I just can't figure out what's going wrong.
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">a</title>
<id>a</id>
<rights type="text">© Crown Copyright 2009</rights>
<updated>2011-01-19T11:23:25Z</updated>
<category term="Service"/>
<author>
<name>c</name>
<uri>http://www.meh.com</uri>
<email>erwt</email>
</author>
<complete xmlns="http://purl.org/syndication/history/1.0"/>
<entry>
<id>http://meh.com/services</id>
<title type="text">title</title>
<updated>2010-06-18T19:52:12+01:00</updated>
<link rel="self" title="title" href="meh"/>
<link rel="alternate" title="title" href="id"/>
<content type="application/xml">
<s:service xmlns:s="http://syndication.nhschoices.nhs.uk/services">
<s:type code="S">h</s:type>
<s:deliverer>j</s:deliverer>
<s:parent>k</s:parent>
<s:treatments>
<s:treatment>fissure</s:treatment>
<s:treatment>fistula</s:treatment>
<s:treatment>liver</s:treatment>
<s:treatment>pancreas</s:treatment>
<s:treatment>Cirrhosis</s:treatment>
<s:treatment>Coeliac disease</s:treatment>
<s:treatment>Crohn's disease</s:treatment>
<s:treatment>Diagnostic endoscopy of the stomach</s:treatment>
<s:treatment>Diverticular problems</s:treatment>
<s:treatment>Gastrectomy</s:treatment>
<s:treatment>Gastroenteritis</s:treatment>
<s:treatment>Gastroenterology</s:treatment>
<s:treatment>Gastroesophageal reflux disease(GORD)</s:treatment>
<s:treatment>Hepatitis</s:treatment>
<s:treatment>Hepatitis A</s:treatment>
<s:treatment>Hepatitis B</s:treatment>
<s:treatment>Hepatitis C</s:treatment>
<s:treatment>Hernia hiatus</s:treatment>
<s:treatment>Ileostomy</s:treatment>
<s:treatment>Irritable bowel syndrome</s:treatment>
<s:treatment>Liver disease (alcoholic)</s:treatment>
<s:treatment>Obesity</s:treatment>
<s:treatment>Pancreatitis</s:treatment>
<s:treatment>Peptic ulcer</s:treatment>
<s:treatment>Peritonitis</s:treatment>
<s:treatment>Primary biliary cirrhosis</s:treatment>
<s:treatment>Surgery for haemorrhoids</s:treatment>
<s:treatment>Therapeutic endoscopy on the stomach</s:treatment>
<s:treatment>Ulcerative colitis</s:treatment>
</s:treatments>
<s:phone>020 8</s:phone>
<s:fax>020 8</s:fax>
<s:email>jj</s:email>
<s:website>oiyi</s:website>
</s:service>
</content>
</entry>
</feed>
As you can see it uses the Atom namespace, Purl syndication namespace and NHS Choices namespace only the NHS Choices namespace actually uses the prefix though, this is what is confusing me really. How would I declare the other namespaces and do I even need to?
Here's my XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://syndication.nhschoices.nhs.uk/services">
<ul>
<xsl:template match="/">
<xsl:for-each select="feed/entry/content/s:service/s:treatments/s:treatment">
<li><xsl:text></xsl:text></li>
</xsl:for-each>
</xsl:template>
</ul>
</xsl:stylesheet>
This XSL was taken from the w3schools example so apologies if it's bare.
Any ideas what I need to do to make this work?
Thanks,
Colin
First of all your XSLT is not valid at all (uloutside xsl:template is not valid XSL).
Further more as there is a default namespace on your feed tag you have also to define this in your xslt. You also better make usage of mathcing templates intead of for-each loops in CSLT.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://syndication.nhschoices.nhs.uk/services"
xmlns:a="http://www.w3.org/2005/Atom" exclude-result-prefixes="a s">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<ul>
<xsl:apply-templates
select="a:feed/a:entry/a:content/s:service/s:treatments/s:treatment" />
</ul>
</xsl:template>
<xsl:template match="s:treatment">
<li>
<xsl:value-of select="." />
</li>
</xsl:template>
</xsl:stylesheet>

How to Import stylesheets in xslt conditionally?

Is there any way to import stylesheets after checking some conditions?
Like,if the value of variable $a="1" then import 1.xsl or else import 2.xsl.
Hi All, Is there any way to import
stylesheets after checking some
conditions?
Like,if the value of variable $a="1"
then import 1.xsl or else import
2.xsl.
No, the <xsl:import> directive is only compile-time.
In XSLT 2.0 one can use the use-when attribute for a limited conditional compilation.
For example:
<xsl:import href="module-A.xsl"
use-when="system-property('xsl:vendor')='vendor-A'"/>
The limitations of the use-when attribute are that there is no dynamic context when the attribute is evaluated -- in particular that means that there are no in-scope variables defined.
A non-XSLT solution is to dynamically change the href attribute of the <xsl:import> declaration before the transformation is invoked:
Parse the xsl stylesheet as an XML file
Evaluate the condition that determines which stylesheet should be imported.
Set the value of the href attribute of the <xsl:import> declaration to the URI of the dynamically determined stylesheet-to-be-imported.
Invoke the transformation with the in-memory xsl stylesheet that was just modified.
I know this post is old, but I want to share my opinion.
Each display could use one template instead of two. The value display will be change with a VB application.
breakfast_menu.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="conditionDisplay.xsl" ?>
<data>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
<display>1</display>
</data>
In this file, I imported my displays and with a condition I tell the template what I need.
conditionDisplay.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:import href="display1.xsl"/>
<xsl:import href="display2.xsl"/>
<xsl:template match="/">
<xsl:variable name="display"><xsl:value-of select= "data/display"/></xsl:variable>
<xsl:choose>
<xsl:when test="$display='1'">
<xsl:call-template name="display1" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="display2 />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
display1.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="display1">
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="data/breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
display2.xsl:
<?xml version="1.0" encoding="UTF-8"?>futur
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="display2">
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#222222">
<xsl:for-each select="data/breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I genuinely apologize for my terrible English. It will be better to the next post and I hope will help someone as I think it's not the best solution.