Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2004, 04:12 AM
Registered User
 
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default "choose, when, otherwise" - problem

Hi,

I'm trying to creating an xslt. Now I've got these tags in xslt (just a short example):
Code:
<xsl:for-each select="./statistics/param">
<td><xsl:value-of select="@nRecs"/></td>
</xsl:for-each>
What I'm trying to do is, that the <xsl:value-of select="@nRecs"/> - tag should only be displayed when it contains something, if it's empty it should show <td>&nbsp;</td>. Here's an example of my idea:

Code:
<xsl:choose>
<xsl:when <xsl:value-of select="@nRecs"/> = " ">
 <td><xsl:value-of select="@nRecs"/></td>
</xsl:when>
<xsl:otherwise>
  <td>&nbsp;</td>
</xsl:otherwise>
</xsl:choose>
Now, of course this is not working, but how exactly do I put the value-of in the when-condition?

Thanks for your help!

 
Old December 13th, 2004, 09:24 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

Hi,
   You should put
        <xsl:when test='@nRecs=""' >
instead of
       <xsl:when <xsl:value-of select="@nRecs"/> = " ">


 
Old December 13th, 2004, 09:52 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

And nbsp isn't a built-in entity, use
Code:
#xa0;
(&-#-x-a-0-;) instead.

--

Joe (Microsoft MVP - XML)
 
Old December 13th, 2004, 09:54 AM
Registered User
 
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Santhi,

thanks for your reply. I tried it (and it would work), but I found out that my whole idea is not working.

But I still got this issue. My problem is the following:

I've got an xml-file like this (just an example to show the idea):

Code:
<statistics>
 <param nRecs="0">
 </param>
 <param1 nRecs = "0">
 </param1>
</statistics>

<statistics>
 <param1 nRecs = "0">
 </param1>
</statistics>
As you can see, one of the <statistics>-tags is containing the <param>-tag one of them not. In my xslt I created a table, where the rows are printed in the for-each-loops:

Code:
<table>
<tr>
<td>param</td>
<td>param1</td>
</tr>
<tr>
<xsl:for-each select="./statistics/param">
   <td><xsl:value-of select="@nRecs"/></td>
</xsl:for-each>
<xsl:for-each select="./statistics/param1">
  <td><xsl:value-of select="@nRecs"/></td>
</xsl:for-each>
</tr>
<table>
Now, the problem is that if the param-tag is missing, it won't print out anything. How can I check, if the param-tag is there?

Thanks for any help!!

 
Old December 13th, 2004, 09:56 AM
Registered User
 
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks joefawcett,
That explaines why it printed out the &nbsp;. sorry for all those silly questions but I'm a total newbie to xsl/xml

 
Old December 13th, 2004, 10:06 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think your loop is too "low down":
Code:
<table>
<tr>
<td>param</td>
<td>param1</td>
</tr>
<xsl:for-each select="statistics">
<tr>
<td>
<xsl:choose>
<xsl:when test="param">
<xsl:value-of select="param/@nRecs"/>
</xsl:when>
<xsl:otherwise>#xa0;</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:choose>
<xsl:when test="param1">
<xsl:value-of select="param1/@nRecs"/>
</xsl:when>
<xsl:otherwise>#xa0;</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
--

Joe (Microsoft MVP - XML)
 
Old December 13th, 2004, 10:22 AM
Registered User
 
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot joefawcett,
It was really the problem of the loop being too "low down". Now it works!!! :D Thanks so much!!

 
Old December 17th, 2004, 01:43 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Other way of using space in xslt: <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old December 17th, 2004, 02:22 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

disable-output-escaping is not supported in all processors. Why use a kludge when there are built in methods that work?

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
choose between two directories and use one jjk2 PHP How-To 1 June 3rd, 2008 05:07 PM
Nested Choose Ryan Moore XSLT 6 November 9th, 2007 03:24 PM
Both "when" hit in choose estranda XSLT 2 June 18th, 2007 09:02 AM
Choose all by date? morpheus SQL Server ASP 3 July 30th, 2005 11:50 PM
xsl:choose djmarquette XSLT 5 March 29th, 2005 11:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.