1

Topic: How in xslt to transform in advance unknown kol-in columns in the table?

Certainly on mind at once comes to use a cycle:

<xsl:for-each
select = Expression
</xsl:for-each>

For example, it is necessary to deduce the data on customers:

<customers>
<customer>
<name> Vanja </name>
<phone> (812 123-4567 </phone>
</customer>
<customer>
<name> Petja </name>
<phone> (812 423-4537 </phone>
</customer>
</customers>

That it to make, it is necessary to organize here such classical cycle:

<xsl:for-each select = "customers/customer">
<TR>
<TD> <xsl:value-of select = "name"/> </TD>
<TD> <xsl:value-of select = "phone"/> </TD>
</TR>
</xsl:for-each>

As a result it is had necessary 2 columns:

Name Phone
--------------------
 (812 123-4567
Petja (812 423-4537

But it is all for an output of the unknown person in advance kol-va lines and if it is not known kol-in columns?
For example, it is necessary to count kol-in made these clients of orders for the period set in the parameter that was here so:

Name Phone K1 K2 K3
----------------------------------
 (812 123-4567 5 3 2
Petja (812 423-4537 2 4 7

In xslt I transfer as parameter kol-in columns, i.e. I know that this time them will be 3 (January, February and March). But how to write the universal code calculated for any value of this parameter?
Let's admit, columns in resultant , containing in orders on months will be named by a certain rule, for example so: QTY_1, QTY_2, QTY_3., QTY_N. And titles of columns in the total table too: 1, 2, 3..., N. (Where - kol-in columns, i.e. months, i.e. value transferred in xslt parameter).
How correctly to organize a cycle. It can be possible as that in a special way to use a tag colspan?

2

Re: How in xslt to transform in advance unknown kol-in columns in the table?

I would like to specify that on an input in conversion XSLT is available XML similar to it:

<customers>
<customer>
<name> Vanja </name>
<phone> (812 123-4567 </phone>
</customer>
<customer>
<name> Petja </name>
<phone> (812 423-4537 </phone>
</customer>
</customers>

And on a conversion output it turns out HTML.
This input XML is formed dynamic (there is a XML file generator, in this generator it is transferred by parameter months), i.e. in the general schematical type it can will such:

<customers>
<months_qty> n </months_qty>
<customer>
<name> Vanja </name>
<phone> (812 123-4567 </phone>
<qty_1> 5 <qty_1>
<qty_2> 3 <qty_2>
<qty_3> 2 <qty_3>
.....
<qty_n> x <qty_n>
</customer>
<customer>
<name> Petja </name>
<phone> (812 423-4537 </phone>
<qty_1> 2 <qty_1>
<qty_2> 4 <qty_2>
<qty_3> 7 <qty_3>
.....
<qty_n> x <qty_n>
</customer>
.................................
</customers>

To process a node </customer>, i.e. variable kol-in customers corresponding to lines <TR> - not a problem, we use a cycle <xsl:for-each>, and here is how to process variable kol-in orders at each customer on the months, corresponding to a cell in line <TD>?
In XSLT conversion I will know in months thanks to a tag <months_qty> from initial XML.

3

Re: How in xslt to transform in advance unknown kol-in columns in the table?

All is simple. Cycles are organized by a recursion, reversal to a dynamic name through a construction

*[name () =....]

[spoiler]

<xsl:template match = "customers">
<table border = "1">
<xsl:variable name = "count" select = "months_qty"/>
<tr>
<th> Name </th>
<th> Phone </th>
<xsl:call-template name = "printHeaders">
<xsl:with-param name = "idx" select = "$ count"/>
</xsl:call-template>
</tr>
<xsl:apply-templates select = "customer">
<xsl:with-param name = "count" select = "$ count"/>
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match = "customer">
<xsl:param name = "count"/>
<tr>
<td>
<xsl:value-of select = "name"/>
</td>
<td>
<xsl:value-of select = "phone"/>
</td>
<xsl:call-template name = "printData">
<xsl:with-param name = "idx" select = "$ count"/>
</xsl:call-template>
</tr>
</xsl:template>
<xsl:template name = "printHeaders">
<xsl:param name = "idx"/>
<xsl:if test = "$ idx> 0">
<xsl:call-template name = "printHeaders">
<xsl:with-param name = "idx" select = "$ idx - 1"/>
</xsl:call-template>
<th>
<xsl:value-of select = "concat (' K ', $idx)"/>
</th>
</xsl:if>
</xsl:template>
<xsl:template name = "printData">
<xsl:param name = "idx"/>
<xsl:if test = "$ idx> 0">
<xsl:call-template name = "printData">
<xsl:with-param name = "idx" select = "$ idx - 1"/>
</xsl:call-template>
<td>
<xsl:value-of select = "* [name () = concat (' qty _ ', $idx)]"/>
</td>
</xsl:if>
</xsl:template>

[/spoiler]

4

Re: How in xslt to transform in advance unknown kol-in columns in the table?

[quote = _ Vasilisk _] All is simple. Cycles are organized by a recursion, reversal to a dynamic name through a construction [code]
*[name () =....]

Thanks! I will try to apply.