Jump to content
xisto Community
Sign in to follow this  
qwijibow

XSL (Help Converting XML To HTML <img Src=> Tags)

Recommended Posts

Hello, for the life of me i cannot work out what is wrong with this XSL

i am trying to convert XML to a scaled image which is also a hyper link to itself (in full size)

here is some example XML that i need to translate with XSLT

<entry date="30th August 2004" title="Ohhh yes !!!">  <small_photo url="incase.jpg"/>  <small_photo url="fstleaf1.jpg"/>  <small_photo url="fstleaf2.jpg"/>  <small_photo url="fstleaf3.jpg"/>  <small_photo url="fstleaf4.jpg"/>  <small_photo url="pants.jpg"/>  <text> 	 Yep! after only a week ! The first piccy is of the plants inside the computer case. 	 The cardboard thing at the top covered in duct tape is part of a domino's pizza box, 	 we cut holes in it, and used to to support the light bulbs.  </text>  <text> 	 The last photo shpws the minni green house fully switched on, in its hiding place. 	 The other computers in the foor drown out the noise of the quiet fans. PERFECTLY hidden !  </text>	</entry>

and here is the XSL template i am trying to do the converting with
<xsl:template match="small_photo">  <a><xsl:attribute name="href"><xsl:value-of match="@url"/></xsl:attribute> 	 <img>    <xsl:attribute name="src"><xsl:value-of match="@url"/></xsl:attribute>    <xsl:attribute name="width">20%</xsl:attribute> 	 </img>  </a>	</xsl:template>

in other words, i am trying to generate the following HTML

<a href="incse.jpg>         <img src="incase.jpg" width="20%"/></a>

When i try to open the XMS document in firefox, i get the following error "Error loading stylesheet: Parsing an XSLT stylesheet failed."

the document is rendered perfectly (without pictures when i remove the small_photo template.

any ideas anyone ??? thankyou.

Share this post


Link to post
Share on other sites

i am trying to convert XML to a scaled  image which is also a hyper link to itself (in full size)


You are getting there and although your code is right, you are just using the wrong functions. There is more than one small photo so you have to use the for-each function as opposed to the match one. I think this is the rule and sure enough when I tried it, it worked. So here is the modified code;


<?xml version="1.0"?><xsl:stylesheet version="1.0"                             xmlns:xsl="http://www.w3.org/1999/XSL/Transform/; <xsl:template match="/"><html><body><xsl:for-each select="entry/small_photo">  <a><xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>       <img>        <xsl:attribute name="src"><xsl:value-of select="@url"/></xsl:attribute>                <xsl:attribute name="width">20%</xsl:attribute>        </img>           </a></xsl:for-each></body></html></xsl:template></xsl:stylesheet>

So if this is like a blog type thing you are doing correct? Well, then you'll need to do a for-each loop to display each entry if you get what I am saying. "Matching" probably won't work. I'm pretty sure that's why your code didn't work, I just know that when you do the for-each loop, it definately works. Let me know if it solves the problem.


Share this post


Link to post
Share on other sites

Great thanks...

However i dont understand why a for-each is needed.
Im new to XSL, but in the past, i have been using templates to do similar things, and its worked fine...

for example... take the XML

<page>  <paragraph>     THIS is a paragraph  </paragraph>  <paragraph>      This is Anouther Paragraph  </paragraph>  <paragraph>      Yet anouther paragraph !  </paragraph><page>

i have ben using templates to convert it like so...

<xsl:template match="page">     <html>         <xsl:apply-tmplates select="paragraph"/>     </html></xsl:template><xsl:template match="paragraph"> <p>    <xsl:value-of select='.'/> </p></xsl:template>

and it has worked perfectly.
There are many different paragraph tags here.
why does this type of re-occuring tags work with a template, but my small_photo tags need a for-each function ????

Thanks for the Help.
Im doing this because i need to learn XSL, the page the example was from is an olg page i made about converting a pc case into a minni greenhouse with artificial lighgting on a timer, and ventilarion... quite an interesting PC mod :D (worked too)

EDIT:
i didnt have my notes with my when i wrote the above example, so i may have confused some match= and select=, but take my work for it, when i write that with my notes handy, it works fine.

thanks again, your modified code works, i just ned to know why fo future exams etc....

Share this post


Link to post
Share on other sites

I see how you use templates, but I have never, and still don't see why I'd want to use them all that much. I program alot in PHP so the XML IF and FOR-EACH statements come as second nature to me and so I use them quite regularly.I tried making template one like you had suggested but I also got the error message. I'll look into the subject more because it has been a year since I learnt XML and even then I wasn't completely "in touch" with how it worked and how to use it to my advantage.

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.