Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: How to build a table? >Thread Next - Re: How to build a table? Re: How to build a table?To: NULL Date: 6/6/2006 9:03:00 AM
anotherron wrote:
> Thanks Anthony and Ivan. Your exmples are very helpful. There is one detail
> that I left out of my original post. (I did not realize at the time that it
> was important.) I actually have to sort the xml data before traversing the
> tree. Specifically the code is:
>
> <xsl:for-each match="parent">
> <xsl:sort select="child"/>
> ...
> </xsl:for-each>
>
> The problem that I am now encountering is that position() uses the
> document's sort order not the order specified by the sort element. Is there a
> method that will use the sort element's order. (I assume that the sort
> element does not actually create a new tree.) Thanks in advance.
The problem is that the for-each filter is applied befor the sort. Move
the position test inside the loop and it should be fine.
But... the following-sibling axis returns nodes in document order so
tha't going to be a problem. The only "proper" solution to this is to
have a second loop to process the nodes in sort order and find the next
one (could be a performance problem with large node sets).
<table>
<xsl:variable name="parents" select="parent"/>
<xsl:for-each select="$parents">
<xsl:sort select="child"/>
<xsl:if select="position() mod 2 = 1">
<xsl:variable name="index" select="position()"/>
<tr>
<td>
<xsl:apply-templates select="child"/>
</td>
<td>
<xsl:for-each select="$parents">
<xsl:sort select="child"/>
<xsl:if select="position() = $index + 1">
<xsl:apply-templates select="child"/>
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
Or, you could use a node-set extension function (if your processor has
one) to seperate your sorted nodes from the source document.
Or, you go back to plan A and use this to insert your TR tags:
<xsl:text disable-output-escaping="yes">
<![CDATA[<tr>]]>
</xsl:text>
The well-formedness rules of XSL ensure that your output is well formed.
This technique allows you to break those rules which could result in
invalid output if you are not careful. So don't go crazy with it.
Ivan
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
