英文:
Iterating For-each loop on the condition provided in the variable in the XSLT
问题
我想让我的for-each循环在变量中提供的条件上工作。
如果Record_Counter的值为7,则for_each应该迭代7次。
注意:我已经创建了一个名为Counter的变量来存储Record_Counter的值。
请查找我尝试过的XSLT代码,但它没有给我想要的结果。
<xsl:variable name="Counter" select="Element[1]/@Record_Counter" />
<xsl:for-each select="$Counter">
<itemGroup ref="ValTechnique">
<reportItem key="yes">
<itemValue>
<xsl:value-of select="@Reporting"/>
</itemValue>
</reportItem>
<reportItem>
<itemValue>
<xsl:value-of select="@Valuation"/>
</itemValue>
</reportItem>
</itemGroup>
</xsl:for-each>
英文:
I want my for-each loop to work on the condition provide in the variable defined.
If Record_Counter has value 7 in it, then for_each should iterate for 7 times.
Note: I have created the variable Counter to store Record_Counter value.
Please find XSLT code which I tried but it is not giving me desired result.
<xsl:variable name= "Counter" select="Element[1]/@Record_Counter" />
<xsl:for-each select="$Counter">
<itemGroup ref="ValTechnique">
<reportItem key="yes">
<itemValue>
<xsl:value-of select="@Reporting"/>
</itemValue>
</reportItem>
<reportItem>
<itemValue>
<xsl:value-of select="@Valuation"/>
</itemValue>
</reportItem>
</itemGroup>
</xsl:for-each>
答案1
得分: 2
在XSLT 2及更高版本中,您当然可以使用1 to $var
表达式,例如<xsl:for-each select="1 to xs:integer($Counter)">
,但在for-each
中,上下文项目是当前处理的整数值,因此任何尝试选择节点,例如<xsl:value-of select="@Reporting"/>
都不会起作用,您需要在for-each
之前添加<xsl:variable name="context-node" select="."/>
,然后在for-each
内部使用<xsl:value-of select="$context-node/@Reporting"/>
。
这可能仍然不够,因为您不太可能希望输出相同的内容 $Counter
次,但您尚未展示输入和所需输出,因此我无法确定您需要/想要进行的其他更改。
英文:
In XSLT 2 and later you can certainly use a 1 to $var
expression e.g. <xsl:for-each select="1 to xs:integer($Counter)">
but that way inside of the for-each
the context item is the currently processed integer value so any attempt to select nodes with e.g. <xsl:value-of select="@Reporting"/>
will not work, you would need to put e.g. <xsl:variable name="context-node" select="."/>
before the for-each
and then use e.g. <xsl:value-of select="$context-node/@Reporting"/>
inside the for-each
.
That might still not suffice as it is unlikely you want to output the same stuff $Counter
times but you haven't show your input and wanted output so I can't tell what other changes you need/want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论