|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
XML to HTML question
Sorry for a lengthy post, but having difficulty wrapping my mind around XSLT.
Given the following XML file (actually RSS-flavor) and stylesheet below, it is producing some ok output via a javascript function that uses the Microsoft.XMLDOM. How do I transform those link tags into html href tags? Basically am just trying to transform the RSS file into html via XSLT and the javascript function. Thanks for any info and apologies for the long verbose post. fv Javascript for accomplishing: <script type="text/JavaScript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); var xslDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = xslDoc.async = false; xmlDoc.load("http://rss.groups.yahoo.com/group/cnfractal_music/rss"); xslDoc.load("rsstest2.xsl"); var strRSS = new String(xmlDoc.transformNode(xslDoc)); document.write(strRSS); </script> rss feed: <?xml version="1.0" encoding="UTF-8" ?> - <rss version="2.0"> - <channel> <title>Some rss feed</title> <link>http://www.some_site.com</link> <description>Some Site</description> - <item> <title>Re: Feedback</title> <pubDate>Thu, 24 Jun 2004 19:13:20 GMT</pubDate> <author>SomeOne</author> <link>http://www.some_site.com/message/9858</link> <guid isPermaLink="true"http://www.some_site.com/message/9858</guid> <description>Hi, this is my message in response to your question</description> </item> </channel> </rss> ANd the following XSLT stylesheet: <?xml version="1.0"?> <xsl:template xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:for-each select="rss/channel"> <div align='center' bgcolor='slategray'> <xsl:value-of select="description" /> <br /> <xsl:value-of select="title" /> <br /> <xsl:value-of select="link" mode="index" /> </div> </xsl:for-each> <xsl:for-each select="rss/channel/item"> <div style="background-color: slategray;" > Title: <xsl:value-of select="title" /> <br /> <xsl:value-of select="link" /> <br /> Date: <xsl:value-of select="pubDate" /> <br /> Author: <xsl:value-of select="author" /> <br /> Description: <xsl:value-of select="description" /> <hr /> </div> </xsl:for-each> </xsl:template>
__________________
...because that is the way we have always done it. We've been doing it like that for 80 Years! (How do we change that mindset?) |
|
#2
|
||||
|
||||
|
Code:
<xsl:for-each select="Root/Child">
<tr>
<th class='title'>SOME TEXT</th>
<td><a href='some1.php?cg={cgc}&cm={cmc}&cc={cc}'>Click here
</tr>
<tr>
<th class='title'>Some More Text</th>
<td><a href='some2.php?cg={cgc}&cm={cmc}&cc={cc}'>Click here</a></td>
</tr>
</xsl:for-each>
__________________
Teflon - The Black <desc>Mark This Up</desc> |
|
#3
|
|||
|
|||
|
Thanks for the reply. I will give that a try and see what heppens.
fv |
|
#4
|
||||
|
||||
|
Those are squiggly brackets btw not parenthesis
If you have more specific questions, I would be more than happy to help. I apologize for not writing anything other than that quick script. -Teflon The Black |
|
#5
|
|||
|
|||
|
Many thanks. I am much more at home with SQL; still awaiting that moment of epiphany when XSLT clicks.
Ok - I seem to be missing a vital step, given this XML: channel> <title>Some rss feed</title> <link>http://www.some_site.com</link> I want to transform that <link>blah</link> to an html link. I tried: <xsl:for-each select="rss/channel/item"> <div style="background-color: slategray;" > Title: <xsl:value-of select="title" /> <br /> <xsl:value-of select="link" /> <br /> <a href='{link}'>{link}</a> ------------------------------- Using the curly brackets, but the link text is {link} and links to a non-existant page like {C://somepath/link} ; so I must need to somehow extract the XML value of the <link> tag into a variable, call it by a name, 'myVar' and then reference {myVar} in the html link perhaps? I am guessing. Struggling with this simple thing... thanks! fv |
|
#6
|
||||
|
||||
|
Are you running the xml/xsl transformation via a server, or are you just accessing it through double clicking it?
What is the actual value of the link that it is giving? if you can that would help. |
|
#7
|
|||
|
|||
|
The actual transformation is being done on the fly,client-side, by javascript:
<script type="text/JavaScript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); var xslDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = xslDoc.async = false; xmlDoc.load("http://rss.groups.yahoo.com/group/cnfractal_music/rss"); xslDoc.load("rsstest2.xsl"); var strRSS = new String(xmlDoc.transformNode(xslDoc)); document.write(strRSS); </script> Now, here is a snippet of the original XML: - <item> <title>Re: I'm back</title> <pubDate>Wed, 30 Jun 2004 00:09:05 GMT</pubDate> <author>PhilJackson</author> <link>http://launch.groups.yahoo.com/group/cnfractal_music/message/9860</link> <guid isPermaLink="true">http://launch.groups.yahoo.com/group/cnfractal_music/message/9860</guid> <description>Hi Gavin and all, Glad to see you back online! < (Happy Shiny P4 2.8GHz) Ah...</description> </item> Here is some of the XSL that deals with it: <xsl:for-each select="rss/channel/item"> <div style="background-color: slategray;" > Title: <xsl:value-of select="title" /> <br /> <xsl:value-of select="link" /> <br /> <a href='{link}'>{link}</a> What is displayed for the link is: {link} and is underlined like a link. This is all on the local machine, so the link points to : file:///c:/cnf/link So, what I need to do is extract the actual url from: <link>http://launch.groups.yahoo.com/group/cnfractal_music/message/9860</link> and use the value in an html href tag. Many thanks, fv |
|
#8
|
||||
|
||||
|
This is working for me without the javascript
I used php to do the actual transformation, but it also worked for me by simply opening the xml file inside of my browser. I am using Firefox 0.9 I myself stay away from accessing files with javascript as it is my belief that it was not meant to, and there are more secure and efficient ways of doing it, ie server-side. Give it a try though and see if you can get it to work It might also be blowing up because of the < inside description, try escaping that as '<' what I changed in the xsl file is: <a href='{link}'><xsl:value-of select="link" /></a> I have a feeling that the javascript transformation is not interpreting the {link} in the desired fashion, whether it is capable of interpreting it as is, I do not know. Hope that helps ya some? Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:for-each select="item">
<div style="background-color: slategray;" >
Title: <xsl:value-of select="title" />
<br />
<xsl:value-of select="link" />
<br />
<a href='{link}'><xsl:value-of select="link" /></a>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?> <?xml-stylesheet type="text/xsl" href="../testing/test.xsl"?> <item> <title>Re: I'm back</title> <pubDate>Wed, 30 Jun 2004 00:09:05 GMT</pubDate> <author>PhilJackson</author> <link>http://launch.groups.yahoo.com/group/cnfractal_music/message/9860</link> <guid isPermaLink="true">http://launch.groups.yahoo.com/group/cnfractal_music/message/9860</guid> <description>Hi Gavin and all, Glad to see you back online! (Happy Shiny P4 2.8GHz) Ah...</description> </item> Code:
<html>
<head>
<script type="text/JavaScript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xslDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = xslDoc.async = false;
xmlDoc.load("http://rss.groups.yahoo.com/group/cnfractal_music/rss");
xslDoc.load("testing/test.xsl");
var strRSS = new String(xmlDoc.transformNode(xslDoc));
document.write(strRSS);
</script>
</head>
<body>
<?php
$xmlfile = "test.xml";
$xslfile = "test.xsl";
$xslthandler = xslt_create() or die("Can't create XSLT handler!");
xslt_set_base($xslthandler, "file:///C:/Inetpub/wwwroot/testing/");
xslt_process($xslthandler, $xmlfile, $xslfile, "hello.html");
xslt_free($xslthandler);
?>
</body>
</html>
I got it working with and without the javascript actually in the html file. It is simply in there but not accomplishing anything. -Teflon |
|
#9
|
|||
|
|||
|
Tried this per your suggestion:
<a href='{link}'><xsl:value-of select="link" /></a> now the URL displays in the page, but it still points to some nonexistant file called {link} so the {} operator is taken litterally and not as a variable. ??? thanks! fv |
|
#10
|
||||
|
||||
|
what does the link look like when you open the xml file up in the browser?
bypassing the javascipt? -Teflon |
|
#11
|
|||
|
|||
|
It just looks like:
<link>http://launch.groups.yahoo.com/group/cnfractal_music/message/9860</link> (browsing to the *rss file on yahoogroups) the value-of phrase is pulling the link text just fine, it is the {link} that is being interpreted literally rather than as a variable. thanks! fv |
|
#12
|
||||
|
||||
|
sorry, what I meant is.
what happens when you add this line to the xml file and then open it up in your browser. thus allowing the browser to do the transformation and not javascript. <?xml-stylesheet type="text/xsl" href="../testing/test.xsl"?> or similiar according to file names and directories what I am trying to do is see if javascript is what is causing the problem. basically if you can copy the .xml and .xsl files that are in my previous post and put them in a folder called testing, with each file named test.xml, test.xsl. Then open the .xml file up in ie? lemme know what that does, if ya can. -Teflon |
|
#13
|
|||
|
|||
|
Copied the xml file, xsl file and html file.
Xml file worked fine html file worked fine with just the javascript made a copy of the cml file and saved as an rss file worked fine changed reference in html file to point to rss worked fine So I have some little glitch in my xsl file..this stuff is hell to debug! many, many thanks for the assistance!!! fv |
|
#14
|
|||
|
|||
|
Ok, changing the first few lines to match yours and added the ending </xsl:stylesheet> tag and it worked!
new: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl utput method="html"/><xsl:template match="/"> was: <?xml version="1.0"?> <xsl:template xmlns:xsl="http://www.w3.org/TR/WD-xsl"> Now I just to figure how to extract that message id from the link: http://launch.groups.yahoo.com/grou...ic/message/9860 and append to another URL to allow replies... many thanks for all the assistance! I hope to be taking an online XML course through work in the near future. XSLT is a strange twist for a mind used to thinking SQL... fv |