Jump to content

Complicated XSLT


Kovo

Recommended Posts

Hi everyone. Im in a bit of a pickle.I need my XSLT file to read a URL in my XML File.As an example:<food><Type>Fruit</Type><Weight>5 Pounds</Weight><Info>http://www.food.com</Info></food>I want my XSLT file to read the http url and display it as a link. Is this possible? It must be?Thanks for your help.

Link to comment
Share on other sites

Of course it is. You can use either <xsl:attribute> or <xsl:variable> to achieve this.Note: In theese examples I presume you want to use <type> as the naming of the link. You myst specify another path if this is not what should be displayed.xsl:attribute

<a>  <xsl:attribute name="href">    <xsl:value-of select="/food/info" />  </xsl:attribute>  <xsl:value-of select="/food/type" /></a>

<xsl:attribute> is an element which gives a certain attribute to the element it's contained in. In this case the attribute is called "href" which is what you need to create a link. The value of that attibute is determened by the content inside the <xsl:attribute> element. In this case, the value will be the URL itself. The other <xsl:value-of> outside the <xsl:attribute> is what you need for the text of the link.xsl:variable

<xsl:variable name="link" select="/food/info" /><a href="{$link}">  <xsl:value-of select="/food/type" /></a>

In this case you declare a variable before the anchor itself. Then by using the "$" you call on the variable by name. The "{" and "}" are placed because this is not supposed to be part of the URL but a calling to a variable. If you call a variable withing an XPath expression, you should avoid the brackets.I personally prefer the variable method (excuse the pun) for few very good reasons:

  • Less code to look at.
  • A variable could be used multiple times in a document. The <xsl:attribute>'s name and value must be typed everywhere that attribute is used.
  • Much easier to see what is going to be outputed as a value and what as an attribute.
  • A variable can hold everything, just as the <xsl:attribute> can. It could be used for everything. You just have to open the variable. Something like
    <xsl:variable name="linktext">This link is going to lead you to <xsl:value-of select="/food/info"></xsl:variable>

  • Variables have many other different usages that may make the XSLT extremely flexible (by that I mean easy to edit). Their possibilities are hard to explain, and even more harder to handle if it gets crowded (one fails- everything fails). Using variables for simple tasks such as this one might make the struggle with a variables less painful on a later stage.

Note on using multiple variables: The variables' declaring order must be exactly like the one each will first be called on a page.

<xsl:variable name="value" select="/food/type" /><xsl:variable name="link" select="/food/info" />

That will produce an error if "link" is used before "value". It will, however, NOT produce an error if "value" is used at least once before "link".

Link to comment
Share on other sites

Thanks! It worked great!!!Heres my second problem. Now that its all done(this is try out, no real data is set yet). Firefox does not format the XML. Only Internet Explorer. If you try to load the XML file with the XSLT stylesheet in IE, it looks great, in forefox, notrhing gets formatted. Whats up with that?XML

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="database.xslt"?><Database>	  <Torrent> 	 <Name>Torrent 1</Name> 	 <Number>1</Number> 	 <Size>4.5GB</Size> 	 <Description>A good program</Description> 	 <DateAdded>April 15 1988</DateAdded> 	 <Type>Windows Application(ISO)</Type> 	 <Link url="http://www.google.com" />  </Torrent>  <Torrent> 	 <Name>Torrent 2</Name> 	 <Number>2</Number> 	 <Size>4.2GB</Size> 	 <Description>A good program also</Description> 	 <DateAdded>April 15 1948</DateAdded> 	 <Type>Windows Game(ISO)</Type> 	 <Link url="http://www.goog3le.com" />  </Torrent>	</Database>

XSLT

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/><xsl:template match="/"><html>	<head>  <title>Torrents</title>	</head>	<body>	<xsl:for-each select="Database/Torrent"><xsl:sort select="number" order="descending" />	<table border="1"> <tr>      <td width="400px"><center><xsl:value-of select="Name"/></center></td>      <td width="50px"><center><xsl:value-of select="Size"/></center></td>    </tr></table>     <table border="1"><tr>     <td width="180px"><center>Added: <xsl:value-of select="DateAdded"/></center></td>      <td width="270px"><center><xsl:value-of select="Type"/></center></td>      </tr>    </table>      <table border="1"><tr>     <td width="420px"><center><xsl:value-of select="Description"/></center></td>      <td width="30px"><center>#<xsl:value-of select="Number"/></center></td>      </tr>   </table>      <table border="1"><tr><xsl:for-each select="Link">     <td width="350"><center><a><xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>Try It</a></center></td>      <td width="100px"><center>Downloads: </center></td>      </xsl:for-each></tr>   </table><br />		</xsl:for-each>	</body></html>	</xsl:template></xsl:stylesheet>

Again this is all just testing...

Link to comment
Share on other sites

It should be

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

Look at your code again... you have set it to method="xml". You should use that(xml) only if you want the output to be another xml document which is then retransformed with another stylesheet.P.S. I made a long speech about the variable and yet you use the attribute. It seems I'm not as convincing as I was hoping for :) .

Link to comment
Share on other sites

It should be
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

Look at your code again... you have set it to method="xml". You should use that(xml) only if you want the output to be another xml document which is then retransformed with another stylesheet.P.S. I made a long speech about the variable and yet you use the attribute. It seems I'm not as convincing as I was hoping for :) .

Thank you man. As soon as I get back to myhome(room) Ill change it. Sorry that I didnt listen lol :) But ur help has been critical for what I need. Ill let you know if it worked (of course)
Link to comment
Share on other sites

I tried it and it worked. Wow, one stupid 3 letter abbreviation did all that. lolthx a lot u have helped me so much, even though I now realize that my questions were quite noobish. good day!

Link to comment
Share on other sites

heheheh. Spoke too soon. This is what firefox gives me once I upload my site to my server

Error loading stylesheet: An XSLT stylesheet does not have an XML mimetype:http://www.mjf88.com/kev/ttab/database.xslt
MY code atm:
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html>	<head>  <title>Torrents</title>  <style>  body {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px;}	table{border-collapse: collapse; border: none;}	td {font-size: 8pt;	border: solid #FF0000 1px;}  </style>	</head>	<body>	<xsl:for-each select="Database/Torrent"><xsl:sort select="number" order="descending" />	<center><table border="0"> <tr>      <td width="400px"><center><xsl:value-of select="Name"/></center></td>      <td width="50px"><center><xsl:value-of select="Size"/></center></td>    </tr></table>     <table border="0"><tr>     <td width="180px"><center>Added: <xsl:value-of select="DateAdded"/></center></td>      <td width="270px"><center><xsl:value-of select="Type"/></center></td>      </tr>    </table>      <table border="0"><tr>     <td width="420px"><center><xsl:value-of select="Description"/></center></td>      <td width="30px"><center>#<xsl:value-of select="Number"/></center></td>      </tr>   </table>      <table border="0"><tr>     <td width="350" align="center"><xsl:for-each select="Link"/><a><xsl:attribute name="href"/><xsl:value-of select="@url"/>Try It</a>       |       <xsl:for-each select="Link2"/><a><xsl:attribute name="href"/><xsl:value-of select="@url"/>Buy It</a></td>      <td width="100px"><center>Downloads: </center></td>      </tr>   </table></center><br />		</xsl:for-each>	</body></html>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Try changing the "xslt" extension of the file to "xsl". This is the standart extension of such files. Some servers accept xslt but others (apparantly) don't. Don't forget to change the referance in the xml file too :) .

Link to comment
Share on other sites

Tried that. Nothing. Internet Explorer 6 shows it just fine. But when it comes to firefox or netscape I get the same error message.Again this is my code:XML:

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="database.xslt"?><Database>	  <Torrent> 	 <Name>Torrent 1</Name> 	 <Number>1</Number> 	 <Size>4.5GB</Size> 	 <Description>A good program</Description> 	 <DateAdded>April 15 1988</DateAdded> 	 <Type>Windows Application(ISO)</Type> 	 <Link url="http://www.mjf88.com/kev/ttab/click.php?id=1" /> 	 <Link2 url="http://www.googles.com" />  </Torrent>  <Torrent> 	 <Name>Torrent 2</Name> 	 <Number>2</Number> 	 <Size>4.2GB</Size> 	 <Description>A good program also</Description> 	 <DateAdded>April 15 1948</DateAdded> 	 <Type>Windows Game(ISO)</Type> 	 <Link url="http://www.mjf88.com/kev/ttab/click.php?id=2" /> 	 <Link2 url="http://www.goog3les.com" />  </Torrent>	</Database>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/">  <html> 	 <head>    <title>Torrents</title>    <style>  body {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px;}	table{border-collapse: collapse; border: none;}	td {font-size: 8pt;	border: solid #FF0000 1px;}  </style> 	 </head> 	 <body>    <xsl:for-each select="Database/Torrent">   	 <xsl:sort select="number" order="descending"/>   	 <center>      <table border="0">     	 <tr>        <td width="400px">       	 <center>          <xsl:value-of select="Name"/>       	 </center>        </td>        <td width="50px">       	 <center>          <xsl:value-of select="Size"/>       	 </center>        </td>     	 </tr>      </table>      <table border="0">     	 <tr>        <td width="180px">       	 <center>Added: <xsl:value-of select="DateAdded"/>       	 </center>        </td>        <td width="270px">       	 <center>          <xsl:value-of select="Type"/>       	 </center>        </td>     	 </tr>      </table>      <table border="0">     	 <tr>        <td width="420px">       	 <center>          <xsl:value-of select="Description"/>       	 </center>        </td>        <td width="30px">       	 <center>#<xsl:value-of select="Number"/>       	 </center>        </td>     	 </tr>      </table>      <table border="0">     	 <tr>        <td width="350" align="center">       	 <xsl:for-each select="Link">          <a>         	 <xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>Try It</a>       	 </xsl:for-each>     |     <xsl:for-each select="Link2">          <a>         	 <xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>Buy It</a>       	 </xsl:for-each>        </td>        <td width="100px">       	 <center>          <script language="Javascript" src="display.php">         	 <!--//-->          </script>Downloads: <script language="Javascript">ccount_display('<xsl:value-of select="Number"/>')</script>       	 </center>        </td>     	 </tr>      </table>   	 </center>   	 <br/>    </xsl:for-each> 	 </body>  </html>	</xsl:template></xsl:stylesheet>

This is puzzling..

Link to comment
Share on other sites

Maybe it's just that the server you are on sucks :) . Try it elswhere. IE doesn't care for MIME types and FF does. In general, what IE does is bad but in this case- better :) .Also, I see you haven't changed the referance in the XML...?

Link to comment
Share on other sites

Maybe it's just that the server you are on sucks :) . Try it elswhere. IE doesn't care for MIME types and FF does. In general, what IE does is bad but in this case- better :) .Also, I see you haven't changed the referance in the XML...?

No I tried what u said... changing to xsl but it didnt work, so i went back to xslt since that wasnt the problem. Ill try on a different server. Hopefully it works.
Link to comment
Share on other sites

Yea Im about to give up. This is what confuses me. If I load the XML file locally on firefox it works great, but when I load the same XML file on firefox from the server it no longer works. So this must be the servers fault right?

Link to comment
Share on other sites

Yea Im about to give up. This is what confuses me. If I load the XML file locally on firefox it works great, but when I load the same XML file on firefox from the server it no  longer works. So this must be the servers fault right?

Isn't that exactly what I said :) ?
Link to comment
Share on other sites

Mozilla requires that you send the XML and XSLT file holding the stylesheet with an XML mimetype (text/xml or application/xml). This is the most common reason why XSLT won't run in Mozilla but will in Internet Explorer. Mozilla is strict in that way.
Just found that little tip. So how would I apply that to my situation? Considering I load the XML file in an iframe on a .shtml file, is there any changes I should be doing to the shtml file?I also discovered that if I remove the "type" attribute from ym xml file
...<?xml-stylesheet type="text/xsl" href="database.xsl"?>...

Firefox no longer shows the error, and shows the data...just not formatted. I no longer think its the server entirley.

Link to comment
Share on other sites

"mimetype" refers to the

type="text/xsl"

stuff. They are also specified in the server's configuration. I don't know exactly why they are needed and what's the difference but I know they are requred and IE doesn't care for them and that's why it shows them. By the way, is it me, or the XSL stylesheet doesn't have an XML prolog? I mean...

<?xml version="1.0" encoding="ISO-8859-1"?>

Link to comment
Share on other sites

"mimetype" refers to the
type="text/xsl"

stuff. They are also specified in the server's configuration. I don't know exactly why they are needed and what's the difference but I know they are requred and IE doesn't care for them and that's why it shows them. By the way, is it me, or the XSL stylesheet doesn't have an XML prolog? I mean...

<?xml version="1.0" encoding="ISO-8859-1"?>

Ic. Well the prolog is there... just not in the version of code i posted before.Do I have to use that encoding? or can it be UTF-8
Link to comment
Share on other sites

Honestly, I'm out of ideas. This is not a code problem scince everything is working locally. Therefore, it's getting out of my competencse. Try asking the host if he knows anything about this. If he could register the mime type of xml and xslt or whatever...

Link to comment
Share on other sites

Honestly, I'm out of ideas. This is not a code problem scince everything is working locally. Therefore, it's getting out of my competencse. Try asking the host if he knows anything about this. If he could register the mime type of xml and xslt or whatever...

Yea, youve done enough man, thx a lot. Its all good, Ill go bother the server providers. Thx take care.
Link to comment
Share on other sites

Yea, youve done enough man, thx a lot. Its all good, Ill go bother the server providers. Thx take care.

If he is really to blame, don't forget to tell that. There are other people here who have the same problem. Like this one for starters. There also might be more...[edit] By the way, I made more posts today, then the whole week combined I think. All in the XSLT forum. This part of the forum is starting to get bussy. I feel warm inside when I think about it :) [/edit]
Link to comment
Share on other sites

Just to update on thsi matter. It was my host. I emailed them about the problem, and they addmitted that they currently dont have support for XSL/T and XML. But it is on their list for future service upgrades.Woot. My hair will no longer fall from too much stress. And thanks for the help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...