Jump to content

Newbie needs help finding an error


johnmich
 Share

Recommended Posts

Hi All, I am hoping you can help out a newbie. I have been scratching my head far too long on this one and it is time to beg for help. I am just not seeing what is wrong! I have a .aspx page that uses the XML control to transform and incorporate an XML file to show a few new products on a home page. I created my .xml file and .xslt file and tested, but all I got was a heading. Knowing something was wrong in my markup I tested the .aspx control using the w3schools Try It xml and xslt http://w3schools.com/xsl/tryxslt.asp?xmlfi...tfile=cdcatalog. It worked perfectly, so then I attempted to adapt their xml and xslt to my needs. Everything seemed to match perfectly but I seem to get the same result—which is basically a What’s new heading and an empty table. Looking at the sample markup and my original markup I am not seeing a lot of differences. Yet the sample works and mine does not. I am attaching my markup so hopefully someone can point me in the right direction. Since this is going into an ASP page, do I need to include the HTML and Body tags, or can I just start off with my DIV?Also, I know the image markup in the first column of the table is incorrect, but I am hoping you can point me in the right direction on how to include that. Not sure if I can use the asp image control or if I need to include the standard html img tag. I don’t want the content authors to have to worry about the path to the image and just want them to provide the filename. I am hoping I can use the transform to incorporate the full path. I am assuming my example is the way I would incorporate this, but it completely breaks the xslt. To see my initial trouble, you will need to remove this.These files are just a baby step for my project, since I am a total beginner. Ultimately, I would like to have many “newitem” elements in the XML file. On the site’s home page I would like to highlight a few of the items using one transform, and then have another page that shows all everything in the XML file. I am thinking I can do this using an attribute in each newitem something like ShowHome=”Y”. Then for the .xslt on the home page somehow filter to only show the items where the value is Y. Since I can’t even get past the basics, I figured I would go ahead and ask for some advice on this while I am at it.Thanks so much for your help!JohnXML File:

<?xml version="1.0" encoding="iso-8859-1"?><whatsnew>  <newitem>	<ItemName>Blue Keychain</ItemName>	<ItemImage>bluekeychaing.png</ItemImage>	<ItemDescription>This is a blue keychain. You have seen a keychain before, do you really need to have a description?</ItemDescription>  </newitem>  <newitem>	<ItemName>Red Keychain</ItemName>	<ItemImage>redkeychaing.png</ItemImage>	<ItemDescription>This is a red keychain.</ItemDescription>  </newitem>  <newitem>	<ItemName>Green Keychain</ItemName>	<ItemImage>greenkeychaing.png</ItemImage>	<ItemDescription>This is a green keychain.</ItemDescription>  </newitem></whatsnew>

XSLT File:

<?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>	  <body>		<div id="whatsnew">		  <h2>What's new</h2>		  <table border="1">			<xsl:for-each select="whatsnew/newitem">			  <tr>				<td>				  <img src="~/highlights/images/<xsl:value-of select="ItemImage"/>" />				</td>				<td>				  <h3><xsl:value-of select="ItemName"/></h3>				  <p><xsl:value-of select="ItemDescription"/></p>				</td>			  </tr>			</xsl:for-each>		  </table>		</div>	  </body>	</html>  </xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Well what you have is not even well-formed XML and any XSLT stylesheet needs to be a well-formed XML document as well.As for setting an attribute with XSLT, the short and preferred way is an attribute value template

  <img src="highlights/images/{ItemImage}"/>

The long way is with xsl: attribute

  <img>	 <xsl:attribute name="src">	   <xsl:value-of select="concat('highlights/images/', ItemImage)"/>	 </xsl:attribute>  </img>

That should give you working code as far as the XSLT is concerned. Getting the paths to the images right is another problem, that depends on the directory structure you have.As for having the XSLT output html and body, no, if you use the ASP.NET Xml control in an .aspx page then the page itself creates that and your XSLT should only output stuff that belongs into the body of an HTML document, e.g. divs, paragraphs, tables.

Link to comment
Share on other sites

Well what you have is not even well-formed XML and any XSLT stylesheet needs to be a well-formed XML document as well.As for setting an attribute with XSLT, the short and preferred way is an attribute value template
  <img src="highlights/images/{ItemImage}"/>

The long way is with xsl: attribute

  <img>	 <xsl:attribute name="src">	   <xsl:value-of select="concat('highlights/images/', ItemImage)"/>	 </xsl:attribute>  </img>

That should give you working code as far as the XSLT is concerned. Getting the paths to the images right is another problem, that depends on the directory structure you have.As for having the XSLT output html and body, no, if you use the ASP.NET Xml control in an .aspx page then the page itself creates that and your XSLT should only output stuff that belongs into the body of an HTML document, e.g. divs, paragraphs, tables.

Thank you for the advice. I was never able to see why it was not well-formed, VS told me it was fine, but I took your suggestion to heart and created a new .xml file from scratch using another tool and it now transforms. Also thank you for the help with insertting the image. VS did not like the preferred method you provided (is that XSLT 2?) but the alternate works like a champ. Thanks so much!
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
 Share

×
×
  • Create New...