Jump to content

XSLT Tutorial: Less-than doesn't work


Gil

Recommended Posts

I'm following the XLT Tutorial at http://www.w3schools...sl/xsl_sort.asp. I have been playing with the wonderful Try-It Editor. (Thank you to whomever invented that thing!) This code works fine:

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>	<h2>My CD Collection</h2>	<table border="1">	  <tr bgcolor="#9acd32">		<th>Title</th>		<th>Artist</th>		<th>Price</th>	  </tr>	  <xsl:for-each select="catalog/cd[price > 8]">	  <xsl:sort select="price"/>	  <tr>		<td><xsl:value-of select="title" /></td>		<td><xsl:value-of select="artist" /></td>		<td style="text-align:right"><xsl:value-of select="price" /></td>	  </tr>	  </xsl:for-each>	</table>  </body>  </html></xsl:template></xsl:stylesheet>

Any number I put to replace the "8" at the end of the for-each line...

	  <xsl:for-each select="catalog/cd[price > 8]">

...works as one might expect. However, if I change the > to < (and nothing else!) in that same line,...

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>	<h2>My CD Collection</h2>	<table border="1">	  <tr bgcolor="#9acd32">		<th>Title</th>		<th>Artist</th>		<th>Price</th>	  </tr>	  <xsl:for-each select="catalog/cd[price < 8]">	  <xsl:sort select="price"/>	  <tr>		<td><xsl:value-of select="title" /></td>		<td><xsl:value-of select="artist" /></td>		<td style="text-align:right"><xsl:value-of select="price" /></td>	  </tr>	  </xsl:for-each>	</table>  </body>  </html></xsl:template></xsl:stylesheet>

...my browser [Firefox version 15.0] reports a parsing error. Why is that, do you suppose? --Gil

Link to comment
Share on other sites

Lawsy! It don't work wid "if" neither! This code works:

	  <xsl:for-each select="catalog/cd">	  <xsl:sort select="price"/>	   <xsl:if test="price > 10"> 	  <tr>		<td><xsl:value-of select="title" /></td>		<td><xsl:value-of select="artist" /></td>		<td style="text-align:right"><xsl:value-of select="price" /></td>	  </tr>	   </xsl:if>	  </xsl:for-each> 

This code gets a parsing error:

	  <xsl:for-each select="catalog/cd">	  <xsl:sort select="price"/>	   <xsl:if test="price < 10"> 	  <tr>		<td><xsl:value-of select="title" /></td>		<td><xsl:value-of select="artist" /></td>		<td style="text-align:right"><xsl:value-of select="price" /></td>	  </tr>	   </xsl:if>	  </xsl:for-each>

Well, if that don't beat all! The onliest thing I changed is: in the 'if' statement I changed ">" to "<". Nothin else! I promise! The error messages in both cases point to the "&" in "<". Can anyone help me usderstand why these don't work? --Gil

Edited by Gil
Link to comment
Share on other sites

As interesting as a that client-side try XSLT tool might be, its implementation is rather poor so the behaviour that you see is simply a shortcoming in the implementation; it takes the source code entered in the two textareas and inserts it in other textareas hidden in an iframe to then parse the textarea contents as XML with client-side Javascript. That way the proper escaping of '<' is unescaped by the browser when Javascript accesses the value pulled from the textarea and the XML parser correctly indicates an XML syntax error. You can encounter the same problem if you use a '&lt' in the XML input source sample. If you want to learn XSLT I would suggest to install an XSLT processor like Saxon 9 or AltovaXML with a command line interface and simply edit the files in a text editor of your choice. Or you might try other online try-it offers like http://xmlplayground.com/index.php (Make sure there you check to checkbox "involve" on the XSLT code sample before hitting the "run" button) but I am not sure they do any better or don't have other shortcomings.

Link to comment
Share on other sites

Wow, Martin, thanks! I guess client-side XSLT just isn't yet ready for me and my projects. I love the concept, but I'm more interested right now in things that work.

the behaviour that you see is simply a shortcoming in the implementation
It's a relief, actually. It tells me that I don't need to learn to use XSLT just now. I can go ahead and implement with XML and POJ (Plain Old JavaScript) -- both of which seemingly have matured enough for me. I'll put client-side XSLT off for another year or so and then check back in. Thanks for the clarity! Thanks also for the suggestions about server-side XSLT. That won't work on my current project, but I'm keeping it in mind. --Gil Edited by Gil
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...