I have the following XML (it is simplified and most attributes are omitted):
<Document>
<Transfer Name="" From="" To=""/>
<Transfer Name="" From="" To=""/>
<OtherElement/>
<OtherElement/>
<Flight AirLina="" From="" To=""/>
<Flight AirLina="" From="" To=""/>
<OtherElement/>
<Hotel Name="" Duration=""/>
<Hotel Name="" Duration=""/>
<OtherElement/>
<OtherElement/>
<Extras Name="" Price=""/>
<Extras Name="" Price=""/>
<Extras Name="" Price=""/>
<Extras Name="" Price=""/>
<Extras Name="" Price=""/>
<Extras Name="" Price=""/>
<OtherElement/>
<OtherElement/>
</Document>
I have a variable, containing different elements:
<xsl:variable name="packageElements"
select="/Document/Transfer | /Document/Coach | /Document/Flight | /Document/Hotel | /Document/Extras" />
I would like to display that data in a table with 2 columns. I am using XSLT1.0 and MSXSL processor.
I have been trying it out with the simplest solution I could think of:
<table>
<tbody>
<xsl:for-each select="$packageElements[position() mod 2 = 1]">
<tr>
<td>
<!-- current element -->
<xsl:value-of select="local-name()"/>
</td>
<td>
<!-- element following the current in the $packageElements variable -->
<!-- Here is where I'm stuck, I can't figure out how to correctly pick it up :( -->
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
Would really appreciate any help.
I think the complexity is in here:
This is a node in $packageElements node-set with a position() greater than current node. But, what is the position of the current node in $packegeElements node-set?
Check this. Dimitre builts a expression wich is the count for intersection between preceding nodes (in the document) of current node and the node-set.
This transformation:
when applied on this XML document:
produces the wanted, correct result:
Alright,
I have combined @Dimitre Novatchev's idea from this post's answers and @Tomalak's from [XSLT]: Rendering a node sequence as M x N table post. I really liked @Tomalak's solution with
$perRow
variable and the<xsl:template name="filler">
template to cope with empty cells.With the idea taken from @Dimitre Novatchev answer I'm holding on to
$trStartPos
. I then calculate$lowerBoundry
and$upperBoundry
that are used to accumulate all elements that should appear in a row. There might be a more elegant way to do this calculation - please let me know if you come up with one, I would really appreciate it!XSLT Transformation
applied on the following XML
results in the following output