简体   繁体   English

在XSLT中,可以在使用参数调用模板的过程中使用xpath表达式的值

[英]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转换,并在其中使用以下代码调用带有参数的模板

<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. 这将调用模板函数,该函数输出HTML中表元素的一部分。 The curRow and curCol are used to determine which row and column we are in the table. curRow和curCol用于确定表格中的行和列。 gbl_maxCols is set to the number of columns in an html table gbl_maxCols设置为html表中的列数

<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. 此函数的结果是一组td元素,但是其中一些元素(具有skipColumn属性大于1的元素跨越了超过1列,我需要在下次调用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. 就像我在简单地增加curCol参数的情况下所期望的那样,但是我有一种情况需要使用数学中xml属性skipColumns中的值来计算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. 但是,在相同情况下,我需要跳过某些列,并且需要从xml属性中传递该值以计算需要跳过多少列。

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. 有什么方法可以将XML页面中的属性用于计算xsl中的param值。

My xml page is something like this (edited heavily since the xml file is rather large) 我的xml页面是这样的(由于xml文件很大,因此进行了大量编辑)

<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><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><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. 我仍然不太确定您要做什么,但是我可以看到您创建$ curCol值时遇到的问题。

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. 选择是将$ curCol的值添加到@skipColumns属性的序列中。 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. 我的理解是,您希望将当前col的编号添加到属性@skipColumns中的值。

Assuming the context node is the 'column' element: 假设上下文节点是“ column”元素:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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