简体   繁体   中英

In XSLT is it possible to use the value of an xpath expression in a call to a template using an param

I am performing an xsl transform and in it I call a template with a param using the following code

<xsl:call-template name="GenerateColumns">
     <xsl:with-param name="curRow" select="$curRow"/>
     <xsl:with-param name="curCol" select="$curCol + 1"/>
</xsl:call-template>

This calls a template function which outputs part of a table element in HTML. The curRow and curCol are used to determine which row and column we are in the table. gbl_maxCols is set to the number of columns in an html table

<xsl:template name="GenerateColumns">
   <xsl:when test="$curCol &lt;= $gbl_maxCols">
       <td>
       <xsl:attribute="colspan">
           <xsl:value-of select="/page/column/@skipColumns"/>
       </xsl:attribute> 
   </xsl:when>
</xsl:template>

The result of this function is a set of td elements, however some of these elements (those with a skipColumn attribute greater than 1 span more than 1 column, I need to skip this many columns with the next call to generateColumns.

this works just like I would expect in the case where I simply increment the curCol param but I have a case where I need to use the value from the xml attribute skipColumns in the math to calculate the value for curCol. In the above case I iterate through all the columns and this works for the majority of my use cases. However in same cases I need to skip over some of the columns and need to pass in that value from the xml attribute to calculate how many columns I need to skip.

My naive first attempt was something like this

<xsl:call-template name="GenerateColumns">
     <xsl:with-param name="curRow" select="$curRow"/>
     <xsl:with-param name="curCol" select="$curCol + /page/column/@skipColumns"/>
</xsl:call-template>

But unforutnately this does not seem to work. Is there any way to use an attribute from an xml page in the calculation for the value of a param in xsl.

My xml page is something like this (edited heavily since the xml file is rather large)

<page>
 <column name="blank" skipColumns="1"/>
 <column name="blank" skipColumns="1"/>
 <column name="test" skipColumns="3"/>
 <column name="blank" skipColumns="1"/>
 <column name="test2" skipColumns="6"/>
</page>

after all of this I would like to have a set of td elements like the following

<td></td><td></td><td colSpan="3"></td><td></td><td colSpan="6"></td>

if I just iterate through the columns I instead end up with something like this which gives me more td elements than I should have

<td></td><td></td><td colSpan="3"></td><td></td><td colSpan="6"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>

Edited to provide more information

I'm still not entirly sure what you're trying to do, but I can see the problem with your creation of the $curCol value.

Your assignment looks like this

<xsl:with-param name="curCol" select="$curCol + **/page/column/@skipColumns**"/>

The select is adding the valu of $curCol to the sequence of @skipColumns attributes. This isn't allowed. You could select a particular attribute and add a value to it, for example

<xsl:with-param name="curCol" select="$curCol + /page/column**[1]**/@skipColumns"/>

or, with a parameter

<xsl:with-param name="curCol" select="$curCol + /page/column**[$index]**/@skipColumns"/>

and then you would get the value you want.

My understanding is that you wish to add the number of the current col to the value in the attribute @skipColumns.

Assuming the context node is the 'column' element:

<xsl:call-template name="GenerateColumns">
 <xsl:with-param name="curRow" select="$curRow"/>
 <xsl:with-param name="curCol" select="number($curCol) + number(@skipColumns)"/>
</xsl:call-template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM