Link to home
Start Free TrialLog in
Avatar of midas_06
midas_06

asked on

Using HTML <img tag in XSLT

I'm attempting to transform an xml doc into a web page containing images.  The src attribute is coming from a browser form element in a ASP page.  The form values are written to an xml file that is transformed on submit.  The transformation works if I don't put in the <img tag.  Any other tag works.
 
What am I doing wrong?

When I use the code below I get a blank html page with no data.


XSL code:

<xsl:for-each select="//photo">
    <tr>
      <td><img src="{@purl}" /></td>
      <td><font color="#FFFFCC" size="3"><xsl:value-of select="caption"/></font></td>
    </tr>
  </xsl:for-each>

Any suggestions or comments would be greatly appreciated.
Avatar of avner
avner

The XSL itself looks valid, can you send an XML with it ?
I wouldn't suggest using the FONT tag since it's deprecated or on the way to there.
Dear Midas;

Hope the following example solves ur problem

sample XML file
****************
<Photo>
     <url>D:\xxx.jpg</url>
</Photo>

****************

To display the above the XSL used is

****<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html"/>
     <xsl:template match="/">
          <xsl:for-each select="/Photo">
               <xsl:variable name="loc"><xsl:value-of select="/Photo/url"/></xsl:variable>
               <tr>
                    <td>
                         <img>
                              <xsl:attribute name="src"><xsl:value-of select="$loc"/></xsl:attribute>
                              <xsl:attribute name="width">100</xsl:attribute>
                              <xsl:attribute name="height">100</xsl:attribute>
                         </img>
                    </td>
               </tr>
          </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>
************************************

Please try the above with ur given problem.

If i am going wrong anywhere or u need any more help do write..

Robin

*****************************************
Dear Midas;

Hope the following example solves ur problem

sample XML file
****************
<Photo>
     <url>D:\xxx.jpg</url>
</Photo>

****************

To display the above the XSL used is

****<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html"/>
     <xsl:template match="/">
          <xsl:for-each select="/Photo">
               <xsl:variable name="loc"><xsl:value-of select="/Photo/url"/></xsl:variable>
               <tr>
                    <td>
                         <img>
                              <xsl:attribute name="src"><xsl:value-of select="$loc"/></xsl:attribute>
                              <xsl:attribute name="width">100</xsl:attribute>
                              <xsl:attribute name="height">100</xsl:attribute>
                         </img>
                    </td>
               </tr>
          </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>
************************************

Please try the above with ur given problem.

If i am going wrong anywhere or u need any more help do write..

Robin

*****************************************
Robin has included an <xsl:output method="html"/> tag in his sheet. Have you got one of these as well?
Avatar of midas_06

ASKER

I do have the output element with method set to html.

When I run Robin's code it seems to simply ignore the xsl completely.  When I used the above code this is the html that results:

<html xmlns:fo="http://www.w3.org/1999/XSL/Format">
     <body>
          <h1>This is a Test Transformation</h1>
     </body>
</html>


xsl:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html"/>
   <xsl:template match="/">
     <html>
       <body>
         <h1>This is a Test Transformation</h1>
         <xsl:for-each select="//Photo">
              <tr>
                   <td>
                        <img>
                             <xsl:attribute name="src"><xsl:value-of select="@purl"/></xsl:attribute>
                             <xsl:attribute name="width">100</xsl:attribute>
                             <xsl:attribute name="height">100</xsl:attribute>
                        </img>
                   </td>
              </tr>
         </xsl:for-each>
       </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

What parser are you guys using?  I'm using msxml4.0.  Do I need to use a different parser?  I did some research and heard that the ms parser doesn't parse xsfo.

I am using msxml3.0 .The problem for me is that i am not getting ur required query.If i kindly elaborate ur query and give the required XML file that u has taken into consideration.

In your last posting midas_06 I don't see what the xmlns:fo has got to do with it. FO is for Formatting Objects and NOT HTML, so remove it completely.

Next: I assume that you EXPLICITLY create the XML DOMs in VB Script(?) on an ASP page, load the XML and XSL into tzhem and apply the transformation. The resultant HTML you send to the browser. Correct me if I am wrong.

I'd expect to see in your code tests that the loading of the XML and XSL were successful (by using the document method parseError and testing it) because this often a great source of small silly errors.

You say you get a blank page when things go wrong. How blank? What do you get when you say ViewSource in the browser? One trick is to add extra HTML to try to find the source of the error :-


<TR><TD>Start of //photo</TD></TR>
<xsl:for-each select="//photo">
   <tr>
     <TD>Test Output</TD>
     <TD><xsl:value-of select="."/></TD>
     <td><img src="{@purl}" /></td>
     <td><font color="#FFFFCC" size="3"><xsl:value-of select="caption"/></font></td>
   </tr>
 </xsl:for-each>
<TR><TD>End of //photo</TD></TR>

My test HTML is written here in capital letters.

HTH

Biggie,

Are you speaking of the xlmns:fo in the namespace declaration in the xsl file, or the one in the HTML code?  The one in the HTML code is created via the transformation.  Obviously not in my xsl file.  Either way, I've tried it with & w/o the namespace declaration for FO.  Still doesn't work.

When I say the page is blank, I mean BLANK.  You are correct about my asp page.  In addtion I actually save the resulting html file to the server.  I then redirect to the transformed html doc.  If you look at the test.html page that is the result, the page is just a blank white page. If I perform this transformation, and do not use the <img> tag, the transformation works, and I get a fully functional html page that I can view in the browser.

I didn't try the parseError method, but I'll see if that gives me anything.
Where ever the FO turns up delete it - it is a red herring.

The transformed text is saved to the server. What is in the file? How many bytes does the file have?
Nothing is in the file.  it is 0kb

OK, then probably a riun time error has occured. How have you set the error handling in your script? You could try putting the transform in a try..catch statement and try catching and printing out the exception.

One minor point: I hope that after creating the DOM objets you set their async properties to false. I have often forgotten to do this with weird results!

I would also suggest that you download the XSL debugger from MS (NSDN Home -> Web & Internet Samples Home -> XSL Debugger) and try using that.

Biggie,

Yeah, I've been burned by not setting my async to false, but not this time.  I'll set up some error handling and download the XSL debugger from microsoft.

I'll let you know if I make any progress.
Biggie,

I ran the XSL debugger.  The transformation worked without a hitch.  So, there's a problem with my asp.  I'm going to run through that now.

Do you think there may be a problem with data types? I'm building the xml doc from a form.  I'm using a file form element to get the paths into the xml.  Do you think there is an issue here?

Wil
Biggie,

BTW, I hope you had a good 4th.

I've isolated the problem down to the transform method in my asp page.  I've changed my code so that the stylesheet declaration reference is written, and then redirect to the xml page I've just created.  This works.

Just one problem, the resulting html page is what I need.  I can't guarantee everybody is going to have the correct parser.  Any thoughts.
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah,

I'm going to attempt a reinstall.

thanks
Biggie,

It finally works!

I had to reinstall the parser.  Something wasn't done correctly on the first install.

Thanks for giving some good "think through" logic. It was worth the points alone.

Wil
Pleasure.