Using XSLT prevent output in a foreach based on a previous nodes value

Hellswinter

New Member
I'm new to XSLT and i've been playing with this most of this morning to get to this point but unsure how to proceed. This is some test XML to represent what I'm trying to do\[code\]<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="http://stackoverflow.com/questions/10786524/style.xslt"?><ResultCollection> <ColumnSet> <Column> <Caption>Site</Caption> <DisplayType>String</DisplayType> </Column> <Column> <Caption>Date</Caption> <DisplayType>String</DisplayType> </Column> <Column> <Caption>Attendee</Caption> <DisplayType>String</DisplayType> </Column> <Column> <Caption>Paid</Caption> <DisplayType>Hidden</DisplayType> </Column> </ColumnSet> <RowSet> <Row> <Site>London</Site> <Date>15/06/2012</Date> <Attendee>James</Attendee> <Paid>Yes</Paid> </Row> <Row> <Site>London</Site> <Date>15/06/2012</Date> <Attendee>John</Attendee> <Paid>Yes</Paid> </Row></RowSet></ResultCollection>\[/code\]And this is the associated XSLT that i'm using to transform it:\[code\]<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><head><style type="text/css">table.resultsCollection{ border-collapse:collapse; text-align:center; margin:5px; padding:2px;}table.resultsCollection tr th,table.resultsCollection tr td{ padding:2px 8px 2px 8px;}</style></head> <body> <table border="1" class="resultsCollection"> <tr bgcolor="#eeeeff"> <xsl:for-each select="ResultCollection/ColumnSet/Column"> <xsl:if test="not(self::*/DisplayType[text()]='Hidden')"> <th><xsl:value-of select="Caption"/></th> </xsl:if> </xsl:for-each> </tr> <xsl:for-each select="ResultCollection/RowSet/Row"> <tr> <td><xsl:value-of select="Site"/></td> <td><xsl:value-of select="Date"/></td> <td><xsl:value-of select="Attendee"/></td> <td><xsl:value-of select="Paid"/></td> </tr> </xsl:for-each> </table> </body></html> </xsl:template>\[/code\]I can't change the XML structure that I'm using as that is beyond my control as i think that would make my life easier. Nonetheless my goal is to make it look readable, hence the XSLT. The problem is that the 'Column' nodes contain a 'DisplayType' which determines if that column should show. With an 'xsl:if' I've made sure that any column with that type won't get displayed. However I can't work out how to follow this through and make sure no associated records are also outputted. If you run these two together you will see the fourth column of the table has no header. But the contents of the the two rows with 'Paid' nodes get outputted regardless.Any ideas how to sync the two? (Any suggestions to prove what I've got is also welcome as this is my first stab at it)Chris
 
Back
Top