Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Dynamically create html? [Thread Next] Re: Dynamically create html?To: NULL Date: 6/5/2006 11:45:00 AM Steve wrote: > Hello. I am creating a table using using xslt which I will run in asp.net > 2.0, but I'm testing it in XmlSpy for now.. I want to "dynamically" start > another column of the table when I reach a certain attribute. Here is error > I get: > > xsl:if closing name element expected > > Here is the code: > > <xsl:template match="Category/Section"> > <xsl:if test="@StartNewColumnInSaveAsTemplate = 'true'"> > </td><td> > </xsl:if> > <h4><xsl:value-of select="@Title"/></h4> > <ul> > <xsl:apply-templates select="FieldDefinition" /> > </ul> > </xsl:template> > > Does anyone have a work-around for this or have a better design? Thanks!! > In order to keep your xsl well formed you have to think differently. Instead of thinking the way you might with ASP - process the data until the end of a colum and insert some tags to start a new one - you need to be thinking in whole nodes - how do I identify and then render a whole column at a time? In this case, what you want to do is identify all the sections in one column and then you can wrap them in td tags. You could start with a template that matches each "start" node - something like you already have: <xsl:template match="Category/Section[@StartNewColumnInSaveAsTemplate = 'true']"> And then use a for-each to process all the following-siblings up to the next "start": <xsl:for-each select="following-sibling::Section[count(preceding-sibling::Section[@StartNewColumnInSaveAsTemplate = 'true'][1] | current()) = 1]> I'll break this down for you. "following-sibling::Section" returns all the Section nodes after the current one. "preceding-sibling::Section[@StartNewColumnInSaveAsTemplate = 'true'][1]" returns the previous "start" node before the node being tested. "| current()" combines it with the current node being processed by the template. If the returned node is the same as the curent node then the combined node set will have 1 node and the [count(...) = 1] predicate will return true. So, what that says is sellect all following Section nodes where the previous "start" node is the current node. Which gives you your whole column to wrap TD tags around. <xsl:template match="Category/Section[@StartNewColumnInSaveAsTemplate = 'true']"> <td> <h4><xsl:value-of select="@Title"/></h4> <ul><xsl:apply-templates select="FieldDefinition" /></ul> <xsl:for-each select="following-sibling::Section[count(preceding-sibling::Section[@StartNewColumnInSaveAsTemplate = 'true'][1] | current()) = 1]> <ul><xsl:apply-templates select="FieldDefinition" /></ul> </xsl:for-each> </td> </xsl:template> You could "simplify" this a bit further by combining the context node in the for-each. <xsl:template match="Category/Section[@StartNewColumnInSaveAsTemplate = 'true']"> <td> <xsl:for-each select=". | following-sibling::Section[count(preceding-sibling::Section[@StartNewColumnInSaveAsTemplate = 'true'][1] | current()) = 1]> <ul><xsl:apply-templates select="FieldDefinition" /></ul> </xsl:for-each> </td> </xsl:template> Note: The above xpath is pretty inefficient. As Anthony said, putting display instructions in your xml is not the way things are supposed to be done. If there is a logical grouping to the data indicated by structure or attributes then it would be more efficient to use that. Alternatively, if you have control of the xml and you have to have display instructions in there then different instructions would help. For example, a column number attribute would simplify the grouping. Ivan | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
