472,145 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 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 4722
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, 413 views)
File Type: txt test.xml.txt (1.3 KB, 340 views)
File Type: txt test.xsl.txt (941 Bytes, 622 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

Post your reply

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

Similar topics

5 posts views Thread by Sorby | last post: by
14 posts views Thread by Brandon Hoppe | last post: by
1 post views Thread by Peter Rilling | last post: by
1 post views Thread by Ryan Moore | last post: by
2 posts views Thread by adMjb | last post: by
1 post views Thread by Indian Offshore Company | last post: by
reply views Thread by Saiars | last post: by

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.