1 Replies - 800 Views - Last Post: 04 December 2009 - 10:02 AM

#1 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

XSL if test problem

Posted 04 December 2009 - 07:23 AM

XML file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>	
 <cd>
 <name id ="4" price = "10" > ABC </name>   
 </cd>
 
 <cd>
 <name id ="2" price = "15" > DEF </name>   
 </cd>
 
 <cd>
 <name id ="1" price = "8" > GHI </name>   
 </cd>
 
 <cd>
 <name id ="3" price = "7" > JKL </name>   
 </cd>

</catalog>





XSL file

<?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>name</th>
	  <th>price</th>
	</tr>
	<xsl:for-each select="catalog/cd/name">
	<xsl:if test="@price &lt; 10">
	  <tr>
		<td><xsl:value-of select="@id"/></td>
		<td><xsl:value-of select="@price"/></td>
		</tr>
	</xsl:if>
	</xsl:for-each>
	</table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>




i get no output when <xsl:if test="@price &lt; 10">

but when i say

<xsl:if test="@price &gt; 10">

i get required results

Why is that?

Is This A Good Question/Topic? 0
  • +

Replies To: XSL if test problem

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: XSL if test problem

Posted 04 December 2009 - 10:02 AM

Hmm... looks ok.

I'd recommend avoiding for-each in XSLT if you can. Applying templates to nodes is preferred and usually cleaner.

This worked:
<?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>
		<h2>My CD Collection</h2>
		<table border="1">
			<tr bgcolor="#9acd32"><th>name</th><th>price</th></tr>
			<xsl:apply-templates select="catalog/cd/name[@price &lt; 10]" />
		</table>
	  </body>
	  </html>
	</xsl:template>

	<xsl:template match="catalog/cd/name">
		<tr>
			<td><xsl:value-of select="@id"/></td>
			<td><xsl:value-of select="@price"/></td>
		</tr>
	</xsl:template>

</xsl:stylesheet>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1