Jump to content

Skipping Blank Fields in for-each


Richbuff
 Share

Recommended Posts

I have the following XML given to me:<Contact> <id>3283</id> <Name>John Doe</Name></Contact><Contact /><Contact /><Contact />Im using <xsl:for-each> mixed with html to display the contacts in a repeating row table. <xsl:for-each select="Contact"> <tr> <td><xsl:value-of select="id"/></td> <td><xsl:value-of select="Name"/></td> </tr> </xsl:for-each>The Results:ID: 3283 Name: John DoeID: Name:ID: Name:ID: Name:I would like the suppress the empty <Contact /> fields so an html row is not displayed. However the XSLT output does create a row for the empty <Contact /> field with empty data.ID: 3283 Name: John DoeSearched Google and was unable to find a solution. Any help would be appreciated. Thank you.

Link to comment
Share on other sites

Make it a conditional statement. Something like:

<xsl:for-each select="Contact"><xsl:if test="id"><tr><td><xsl:value-of select="id"/></td><td><xsl:value-of select="Name"/></td></tr></xsl:if></xsl:for-each>

This should make the tr and td only if an ID element exist. Otherwise it will do nothing.

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...