Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Dynamically create html?

From: Ivan Peters <ivan@--.----.--->
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




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent