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 September 8th, 2006, 01:48 PM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditional Output on childnodes?

So I have some XML that looks like this

<Limbs>
  <Limb Name="Shoulder">
    <Value Movement="Flexion" Left="80" Right="100" />
    <Value Movement="Extension" Left="" Right="" />
    <Value Movement="Abduction" Left="80" Right="80" />
    <Value Movement="Adduction" Left="" Right="" />
  </Limb>
  <Limb Name="Elbow">
    <Value Movement="Flexion" Left="" Right="" />
    <Value Movement="Extension" Left="" Right="" />
    <Value Movement="Pronation" Left="" Right="" />
    <Value Movement="Supination" Left="" Right="" />
  </Limb>
</Limbs>

Transform that looks like this

<xsl:for-each select="/Limb">
<p>
<table width="300" border="1">
  <tbody>
   <tr>
    <td colSpan="4">
    <xsl:value-of select="@Name"/>
    </td>
   </tr>
   <tr/>
   <tr>
     <td width="150">Movement</td>
     <td width="50">Left</td>
     <td width="50">Right</td>
  </tr>
   <xsl:for-each select="*">
    <xsl:if test="(@Left != '') or (@Right != '') ">
    <tr>
    <td>
      <xsl:value-of select="@Movement"/>
        </td>
    <td>
    <xsl:value-of select="@Left"/>
    </td>
    <td>
    <xsl:value-of select="@Right"/>
    </td>
    </tr>
    </xsl:if>
   </xsl:for-each>
  </tbody>
</table>

--- Output ---

Shoulder
Movement Left Right
Flexion 80 100
Abduction 80 80

Elbow
Movement Left Right

-----

So it works fine and I get a table for each Limb node (2 in this example), except that if there are no values supplied for the elbow movements the table is printed out with the table headings and no rows. I want to make it such that the /Limb template outputs only if at least one child <Value> node has a 'Left' or 'Right' value other then empty string.



 
Old September 9th, 2006, 04:36 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>So it works fine and I get a table for each Limb node

match="/Limb" will only match a Limb element at the outermost level of the document. No such element exists in your source, so I can't see how you get any output at all.

>I want to make it such that the /Limb template outputs only if at least one child <Value> node has a 'Left' or 'Right' value other then empty string.

Use

xsl:if test="Value[string(@Left) or string(@Right)"



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 10th, 2006, 09:38 PM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't get where this xsl:if would go?

Its actually
<xsl:template match="Limbs">
  <xsl:for-each select="Limb">
    ....
    Generate Table
    Process <Value> child nodes into rows
  </xsl:for-each>
</xsl:template>

I have this line
<xsl:if test="(@Left != '') or (@Right != '') ">
which does not display the row output if both values are empty, but that's not the question. I don't want to write out anything in the entire

<xsl:for-each select="Limb"></xsl:for-each>

loop if all the Left and Right attributes of <Value> are empty string (ie. No table heading).



 
Old September 11th, 2006, 11:49 AM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Solved my own issue, but for those interested or who could suggest a better alternative here is the XSLT.

<xsl:template match="Limbs">
<xsl:for-each select="child::Limb[child::Value[@Left !='' or @Right != '']]">
<table width="300" border="1">
  <tbody>
   <tr>
    <td colSpan="4">
    <xsl:value-of select="@Name"/>
    </td>
   </tr>
   <tr/>
   <tr>
     <td width="150">Movement</td>
     <td width="50">Left</td>
     <td width="50">Right</td>
  </tr>
   <xsl:for-each select="*">
    <xsl:if test="(@Left != '') or (@Right != '') ">
    <tr>
    <td>
      <xsl:value-of select="@Movement"/>
        </td>
    <td>
    <xsl:value-of select="@Left"/>
    </td>
    <td>
    <xsl:value-of select="@Right"/>
    </td>
    </tr>
    </xsl:if>
   </xsl:for-each>
  </tbody>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find no.of childnodes of a tag element? sachin635 .NET Framework 3.5 0 September 15th, 2008 04:32 AM
conditional IF roy_mm Reporting Services 0 October 30th, 2006 07:52 AM
DOM childNodes property : IE and Mozilla Firedrill Javascript 2 June 8th, 2005 11:01 AM
C# Conditional statements sporkman43 General .NET 2 July 23rd, 2004 09:10 AM
conditional output AndersR XSLT 1 August 15th, 2003 12:01 AM





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