Nov 22, 2009

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

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > Miscellaneous Scripting Languages & Ideas

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

qwijibow
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
CODE
<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
CODE
<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

CODE
<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.

 

 

 


Comment/Reply (w/o sign-up)

rkage
QUOTE (qwijibow @ Mar 17 2005, 05:07 PM)
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;

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.

 

 

 


Comment/Reply (w/o sign-up)

qwijibow
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

CODE
<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...

CODE
<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 smile.gif (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....

Comment/Reply (w/o sign-up)

rkage
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.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for xsl, converting, xml, html, img, src, tags
advertisement



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

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com