Jump to content

creating hyperlink with xslt


nova

Recommended Posts

Hi all,I was wondering if anyone can help me out with creating a hyperlink in xslt.I'm using xslt to select certain parts from rss feeds, convert them to xml documents and use a stylesheet to present them nicely in a browser.Now everything is working fine except for the part where I want to extract the link from the rss feed and make it show up in my own xml document.The text for the link is there but for some reason it is not clickable. I use the following code for the link part: <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="channel/link"/> </xsl:attribute> Website </xsl:element>As I said earlier the text "Website" shows up but thats about it, i'm not able to click it.I checked a lot of tutorials but they all seem to tell me this is the correct way. Does anyone know what i'm doing wrong here?Arthur

Link to comment
Share on other sites

Can you show your full XSLT? Perhaps you aren't getting the link URL correctly. And an empty URL only reloads the current page (and so it would appear as if you haven't clicked anything).Note also that you can simply write

<a href="{channel/link}">Website</a>

but that is also not going to work if the XPath statement is wrong.

Link to comment
Share on other sites

Thanks for the fast response! Here is the complete code:<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" version="1.0"><xsl:template match="//rss"> <xsl:processing-instruction name="xml-stylesheet"> href="rss3.css" type="text/css" </xsl:processing-instruction> <xsl:element name="feed"> <xsl:element name="titel"> <xsl:value-of select="channel/title"/> </xsl:element> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="channel/link"/> </xsl:attribute> Website </xsl:element> <xsl:for-each select="channel/item"> <xsl:element name="kop"> <xsl:value-of select="title"/> </xsl:element> <xsl:element name="tekst"> <xsl:value-of select="description"/> </xsl:element> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="link"/> </xsl:attribute> Meer... </xsl:element> </xsl:for-each> </xsl:element> </xsl:template></xsl:stylesheet>All is working except for the link bit. Oh and it's not reloading the page, it's just not giving me anything to click on.

Link to comment
Share on other sites

The xml-stylesheet addition to the output turns the document into an XML document. In XML, there's no links... at least not in the (X)HTML sence.And there's no feed element in (X)HTML either.... nor "kop" nor "tekst". What are you trying to generate anyway? An (X)HTML document, or another custom XML document? You're doing the second currently, but there are no links on that.Why not generate an (X)HTML document? Like so:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/rss"><html><head><title>RSS feed</title></head><body><xsl:apply-templates/></body></html></xsl:template><xsl:template match="channel"><div><h1><a href="{link}"><xsl:value-of select="title"/></a></h1><ul><xsl:apply-templates select="item" /></ul></div></xsl:template><xsl:template match="item"><li><h2><a href="{link}"><xsl:value-of select="title" /></a></h2><div><xsl:value-of select="description"/></div></li></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

At first I did create an XHTML file like you suggested but it turned out I was not allowed to use any html tags (it's an assignment).The feed, kop and tekst elements are there in order for me to make a stylesheet.Basicly what im trying to do: I have an rss feed from which I need to extract only certain information using xslt and then put it in a new xml file. The information extraction works fine, so does the layout (we need to create a stylesheet for the new xml file aswell) but only the links extracted from the rss are not clickable.I assume there is a way to make this work without using xhtml . I tried emailing my teacher to see if this is actually possible or that he made a mistake in the assignment but appearently he is gone for week or so :)Do you or anobody else have any idea on how to do this without the use of xhtml?

Link to comment
Share on other sites

Can you explain in more detail how exactly you use XSLT, do you run that on the server or in the browser? And which browsers do you target? What kind of XML format is that you want to create? Mozilla browsers support simple XLinks and I think you could even get away in Mozilla browsers by simply putting your 'a' elements in the XHTML namespace e.g.

<a href="{channel/link}" xmlns="http://www.w3.org/1999/xhtml">Website</a>

but that will not help with current IE versions.

Link to comment
Share on other sites

Hi Martin,Im pretty new at xml, xslt etc. so I don't really understand what you mean when you asked what xml format it is i want to create.I'm just converting an xml file (rss feed) to another one with xslt, leaving out certain parts of information i'm not interested in.I have three files: an xml file with the rss feeds, an xsl file which converts the xml file to another xml file and finally a css file for styling.We're supposed to open the original xml file (the one with the rss feeds) in a browser and with the use of the xsl file it should output only certain parts of the original xml accompanied with some nice styling.The code you mentioned works like a charm! We're using Mozilla so as long as it works with that browser i'm basicly happy.As i mentioned we're told not to use any html-tags so i guess this code is allowed.Thanks a lot for the help!

Link to comment
Share on other sites

When we say "XML format that you're trying to create", we're reffering to the targeted language. RSS is one language, XHTML is another language, etc.The reason the term "XML format" is used as opposed to simply "language" is that "language" is what's defined as a standard format by someone. The two mentioned langauges are formally defined formats, that can be read by multiple programs (browsers and others), whereas if you created your own XML language, browsers won't be able to understand what you mean, and will treat you as they treat any XML document (including the mentioned languages) without any additional, language specific rules (like the link creation with XHTML's "a" elements).You can use XLink to create links in a custom XML format, yours included. Check the page for example on how to do it. I believe it should work in Firefox, though it doesn't work in IE ('cause XLink itself is an XML format... intended to be used in other XML formats, but still).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...