473,378 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

replaceing a href in RSS link with xsl regex

9
Hello I would like to replace an a href link that is provided in the RSS below with my own link. The link that I am looking to replace is defined in the <description> tag within the RSS. Im guessing I need to use some sort of function in combination with regex.

I am able to create a regex to find and replace the a href link very easily; however, using regex in combination with a find and replace in XSL i am extremly novice.

I am wanting to replace the <description> a href:
Expand|Select|Wrap|Line Numbers
  1. a href='http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show'
  2.  
With this a href:
Expand|Select|Wrap|Line Numbers
  1. a href='javascript:void(kyteplayer.setURI('{$URIVal}'))'
  2.  
XML snippet:
Expand|Select|Wrap|Line Numbers
  1. <item>
  2. <title>New Show: Live Mobile Show</title>
  3. <link>http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show</link>
  4. <guid>http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show</guid>
  5. <description>&lt;a href='http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show'&gt;&lt;img src='http://media08.kyte.tv/store/005/08/crr/0808/02/23/689407-0-47639mobile_689407_120_90-tom-5000.jpg?aid=21808&amp;h=44fade2ef70a4df7e890f5f77c87f5d9' title='Live Mobile Show' width='120' height='90' alt='Live Mobile Show' border='0'/&gt;&lt;/a&gt;&lt;br&gt;produced by tenofquad.</description>
  6.  
  7. <pubDate>Sat, 02 Aug 2008 23:18:20 +0000</pubDate>
  8. <media:content url="http://www.kyte.tv/flash.swf?appKey=MarbachViewerEmbedded&amp;uri=channels/39174/185592&amp;layoutMode=default" fileSize="30" type="application/x-shockwave-flash" height="425" width="425" duration="1"/>
  9. <enclosure url="http://www.kyte.tv/flash.swf?appKey=MarbachViewerEmbedded&amp;uri=channels/39174/185592&amp;layoutMode=default" length="30" type="application/x-shockwave-flash"/>
  10. </item>
  11.  
My XSL snippet:
Expand|Select|Wrap|Line Numbers
  1.      <xsl:template match="item">     
  2.  
  3.         <xsl:variable name="item_link" select="link"/>
  4.  
  5.         <xsl:variable name="item_enclosure" select="enclosure/@url"/>
  6.  
  7.         <xsl:variable name="item_title" select="description"/>
  8.  
  9.         <xsl:variable name="item_media" select="media"/>
  10.  
  11.  
  12.       <xsl:variable name="afterURI">
  13.  
  14.       <xsl:value-of select = "substring-after($item_enclosure, '&amp;uri=')" />
  15.  
  16.       </xsl:variable> 
  17.  
  18.              <xsl:variable name="URIVal">
  19.  
  20.               <xsl:choose>
  21.  
  22.                   <xsl:when test="contains($afterURI, '&amp;')">
  23.  
  24.                      <xsl:value-of select="substring-before($afterURI, '&amp;')"/>
  25.  
  26.                   </xsl:when>
  27.  
  28.                   <xsl:otherwise>
  29.  
  30.                     <xsl:value-of select="$afterURI"/>
  31.  
  32.                   </xsl:otherwise>
  33.  
  34.                </xsl:choose>
  35.  
  36.             </xsl:variable>      
  37.  
  38.         <div class="subHead" style="width:500px;">
  39.           <a href="javascript:void(kyteplayer.setURI('{$URIVal}'))"><xsl:value-of select="title" disable-output-escaping="yes"/></a></div>
  40.  
  41.           <div style="width:500px;">
  42.  
  43.        <xsl:value-of select="description" disable-output-escaping="yes"/><br/></div>
  44.  
  45.  
  46.        (<xsl:value-of select="pubDate"/>)<br/> <hr/>
  47.  
  48.        </xsl:template>
  49.  
Please any help would be great!
Aug 11 '08 #1
11 4916
Dormilich
8,658 Expert Mod 8TB
Maybe I've misunderstood you... you want to replace the href (ok), but what should the output be? (rss again or html)

for a simple replace I'd try
Expand|Select|Wrap|Line Numbers
  1. <xsl:element name="a">
  2.   <xsl:attribute name="href">
  3.     <xsl:text>javascript:void(kyteplayer.setURI('{</xsl:text>
  4.     <xsl:value-of select="child::a/attribute::href"/>
  5.     <xsl:text>}  '))</xsl:text>
  6.   </xsl:attribute>
  7.   <xsl:value-of select="title" disable-output-escaping="yes"/>
  8. </xsl:element>
  9. // or shorter
  10. &amp;lt;a href="javascript:void(kyteplayer.setURI('{<xsl:value-of select="child::a/attribute::href"/>}  '))"&amp;gt;
  11. <xsl:value-of select="title" disable-output-escaping="yes"/>
  12. &amp;lt;/a&amp;gt;
for replacing the old rss with the new rss you might to have a look here: XSL replacing …
Aug 28 '08 #2
coflo
9
Ahhh sorry, IT SHOULD OUTPUT HTML as with the rest of my XSL.


Also I am not trying to replace the href in <TITLE> but in <DESCRIPTION>

to replace the href in the title with the variable i already have that working, although this is another great solution.

Rather i am trying to replace the href in the <DESCRIPTION> with the same virable.

It may be an easier approach since I can already assign the link with the variable in it to just strip out the img src of the <DESCRIPTION> and wrap it with the correct href?

I hope this makes sense!?!?!
Aug 28 '08 #3
Dormilich
8,658 Expert Mod 8TB
Also I am not trying to replace the href in <TITLE> but in <DESCRIPTION>
sorry, but I don't get the point here... as far as I can see there's no href inside <title>.
Rather i am trying to replace the href in the <DESCRIPTION> with the same virable.
but since you want to output html, maybe "replace" is not the best description (I have to admit it confuses me a little). Furthermore if you want the img as link content you may use <xsl:copy> (and of course feel free to tweak the xpath as you need it)
just for clarity... you want to use the uri from <enclosure> and the img of <description>? the thing to take care of is that you use the right xpath expressions (for the uri you have this set up and the img you can access with "item/description/a/img", just put these two in your template and you've got it)
Aug 28 '08 #4
coflo
9
Interesting, that would make the most sense, sorry if I have been confusing - its because i wasn't quit sure of the correct way to go about this... by setting that xpath it doesn't seem to retrieve the image - i actually do not get anything an error or any data.

Perhaps an actual example of how you would implement it in my template would help cause thus far it seems to be nothing. I tried google-ing to see if I could figure out how to grab that xpath in <description> but nothing concrete comes up.
Aug 29 '08 #5
Dormilich
8,658 Expert Mod 8TB
ok, let's assume it is RSS 2.0
the xml snippet (just to have it at hand)
Expand|Select|Wrap|Line Numbers
  1. <item>
  2.     <title>New Show: Live Mobile Show</title>
  3.     <link>http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show</link>
  4.     <guid>http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show</guid>
  5.     <description>
  6.         <a href='http://www.kyte.tv/ch/39174-flocam/185592-live-mobile-show'>
  7.             <img src='_img_source_' title='Live Mobile Show' width='120' height='90' alt='Live Mobile Show' border='0'/>
  8.         </a>
  9.         <br>
  10.         produced by tenofquad.
  11.     </description>
  12.     <pubDate>Sat, 02 Aug 2008 23:18:20 +0000</pubDate>
  13.     <media:content url="_same_as_enclosure_url_" fileSize="30" type="application/x-shockwave-flash" height="425" width="425" duration="1"/>
  14.     <enclosure url="http://www.kyte.tv/flash.swf?appKey=MarbachViewerEmbedded&uri=channels/39174/185592&layoutMode=default" length="30" type="application/x-shockwave-flash"/>
  15. </item>
the html snippet we want to create
Expand|Select|Wrap|Line Numbers
  1. <div class="subHead"> // I dropped the style here because you can define that in CSS
  2.     <a href="javascript:void(kyteplayer.setURI('channels/39174/185592'))">
  3.         <img src='_img_source_' title='Live Mobile Show' width='120' height='90' alt='Live Mobile Show'> // you should define img border in CSS too
  4.     </a>
  5. </div>
note: it is not absolutely clear to me what you want to put inside the link, for now I assume the image.

you see the content marked bold? (I call this content mapping) we now have to find a xsl way to transform one towards the other.
Essentially I do it like this: replace the content we want to have by an appropriate xsl element along with its xpath expression. The difficult part here is to get the XPath right.

step 1 - get the <item> element (as it is the nearest ancestor to both targets (the image and the href) so we can use it as base node)
step 2 - find an XPath expression that matches our targets (we start from <item> now)
step 3 - build the html around that (the html markup not in bold)
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="//item"> <!-- step 1 -->
  2. <!-- define the uri value we need -->
  3.     <xsl:variable name="URIVal"> <!-- step 2 -->
  4.         <xsl:choose>
  5.             <xsl:when test="contains(enclosure/@url, '&')">
  6.                <xsl:value-of select="substring-before(enclosure/@urlI, '&')"/>
  7.             </xsl:when>
  8.             <xsl:otherwise>
  9.               <xsl:value-of select="enclosure/@url"/>
  10.             </xsl:otherwise>
  11.          </xsl:choose>
  12.       </xsl:variable>
  13. <!-- start defining the html -->
  14. <div class="subHead"> <!-- step 3 -->
  15.     <a href="javascript:void(kyteplayer.setURI('{$URIVal}'))">
  16.         <xsl:copy-of select="child::description/descendant::img"> <!-- step 2 -->
  17.     </a>
  18. </div>
  19. </xsl:template>
it is relatively easy to get the image, but for the uri (or rather its part we want) it is not that easy, but you have managed a way to retrieve it (congratulaions!).

you see how we have mapped the content from the xml to the html (or vice versa)?

I hope this will help you

PS: XPath axes provide a comfortable way to move inside the DOM tree
Aug 29 '08 #6
Dormilich
8,658 Expert Mod 8TB
by setting that xpath it doesn't seem to retrieve the image - i actually do not get anything an error or any data.
well it was rather a sketch than a correct XPath. I meant to give you an idea how to get the image. You can use even something like select="descendant::img" (assuming there is only one image in the item)
Aug 29 '08 #7
coflo
9
well it was rather a sketch than a correct XPath. I meant to give you an idea how to get the image. You can use even something like select="descendant::img" (assuming there is only one image in the item)
Ya i understand the idea, but that is why i posted here.........i have no idea how to select it.....it took me forever, with alot of help......to get the uri and turn it into a variable....i am seeing a more clear way of selecting the image but still can't get it to seem to work.
Aug 29 '08 #8
Dormilich
8,658 Expert Mod 8TB
i am seeing a more clear way of selecting the image but still can't get it to seem to work.
after trying by myself I had no trouble getting the image. (see attached files)
note: there was no visible output, but the code proves the correctness.
Attached Files
File Type: txt test.html.txt (389 Bytes, 430 views)
File Type: txt test.xml.txt (1.3 KB, 429 views)
File Type: txt test.xsl.txt (941 Bytes, 648 views)
Aug 31 '08 #9
coflo
9
after trying by myself I had no trouble getting the image. (see attached files)
note: there was no visible output, but the code proves the correctness.
Two things that are apparant when using this code.
1. It gives the error cannot 'xsl:param' cannot be a child of the 'xsl:template' element.

2. My RSS feed does not look like the feed that you have created. The <description> is not written the way you have displayed it other wise this would have been an easy task. look at the actual RSS feed to see the example: http://www.kyte.tv/channels/rss.html?uri=channels/39174

As you can see its not as clear cut defining the im src as given in the example you have sent to me, thus why this code does not work. This is why i thought it would be wise to use some sort of regex to pick out the img src, i just can't figure it out!
Sep 2 '08 #10
Dormilich
8,658 Expert Mod 8TB
some notes

1. this is not according to the specification of the W3C (chapter 11 and 2.2). I only switched to xsl:param for tryout, xsl:variable should work as well.

2. well, you never mentioned the link would be escaped. but roughly, I'd do something like that of the $urival.

regards
Sep 3 '08 #11
Dormilich
8,658 Expert Mod 8TB
I didn't go for a variable, eventually. But I got this working (without a regex, regex is not part of xslt 1.0)
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="//item">
  2.     <xsl:param name="URIVal">
  3.  ...
  4.       </xsl:param>
  5.  
  6. <div class="subHead">
  7.     <a>
  8.         <xsl:attribute name="href">javascript:void(kyteplayer.setURI('<xsl:value-of select="$URIVal"/>'))</xsl:attribute>
  9.         <xsl:element name="img">
  10.             <xsl:attribute name="src">
  11.                 <xsl:call-template name="getAttr">
  12.                     <xsl:with-param name="attrName">src</xsl:with-param>
  13.                 </xsl:call-template>
  14.             </xsl:attribute>
  15. // define the other attributes accordingly, maybe there is a kind of foreach/array possible
  16.         </xsl:element>
  17.     </a>
  18. </div>
  19. </xsl:template>
  20.  
  21. <xsl:template name="getAttr">
  22.     <xsl:param name="attrName"/>
  23. // make sure to match the attribute not an url variable
  24.     <xsl:param name="pattern">
  25.         <xsl:text> </xsl:text>
  26.         <xsl:value-of select="$attrName"/>
  27.         <xsl:text>='</xsl:text>
  28.     </xsl:param>
  29. // extract string from $pattern up to the next space
  30.     <xsl:param name="help" select="substring-before(substring-after(description, $pattern), ' ')"/>
  31. // remove ' from string (unfortunately replace doesn't work because ' is a string delimiter) and print result
  32.     <xsl:value-of select="substring($help, 1, string-length($help)-1)"/>
  33. </xsl:template>
this sure is a tough task.

regards
Sep 3 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sorby | last post by:
Hi I've been coding in PHP for a little while now and was starting to feel pretty confident but then realise I need to understand regular expressions to solve a particular problem I've got ......
14
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only....
2
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display...
2
by: yogesh.bhardwaj | last post by:
Hello all I am trying to modify the link URL when a user clicks on the link. Following is the code: <a onClick="document.location.href=modifyURL(this.href);" href="SOME_DYNAMIC_URL"...
3
by: ningjun.wang | last post by:
Hello: My html file contains the following image link: <a href="some_url"><img src="MyImage.gif"></a> How can I use Javascript to find out the value of some_url for the given image name...
1
by: Peter Rilling | last post by:
I am trying to write a regular expression that locates href attributes in some html content. I used the example in the .NET documentation as a starting point...
1
by: Ryan Moore | last post by:
I am trying to write a regex expression which extracts all href links from a HTML page... I'm currently using the following: href\\s*=\\s*(?:\"(?<1>*)\"|(?<1>\\S+)) but it has a problem with...
2
by: adMjb | last post by:
Hi Any help would be fantastic I have a simple problem but I cant work it out... DOHH...., this is my XML: <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/css"...
1
by: Indian Offshore Company | last post by:
Hi, I want to parse html with multiple <a href.. text...</atags as follow: "... some html... <a class="1" href="city1.html" onclick="etc."click for info on city1 </a.. some html.. <a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.