Iterating For-each loop on the condition provided in the variable in the XSLT

huangapple go评论60阅读模式
英文:

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.

    &lt;xsl:variable name= &quot;Counter&quot; select=&quot;Element[1]/@Record_Counter&quot; /&gt;
&lt;xsl:for-each select=&quot;$Counter&quot;&gt;
        &lt;itemGroup ref=&quot;ValTechnique&quot;&gt;
      &lt;reportItem key=&quot;yes&quot;&gt;
        &lt;itemValue&gt;
		&lt;xsl:value-of select=&quot;@Reporting&quot;/&gt;
		&lt;/itemValue&gt;
      &lt;/reportItem&gt;
      &lt;reportItem&gt;
        &lt;itemValue&gt;
		&lt;xsl:value-of select=&quot;@Valuation&quot;/&gt;
		&lt;/itemValue&gt;
      &lt;/reportItem&gt;
    &lt;/itemGroup&gt;
	&lt;/xsl:for-each&gt;

答案1

得分: 2

在XSLT 2及更高版本中,您当然可以使用1 to $var表达式,例如&lt;xsl:for-each select=&quot;1 to xs:integer($Counter)&quot;&gt;,但在for-each中,上下文项目是当前处理的整数值,因此任何尝试选择节点,例如&lt;xsl:value-of select=&quot;@Reporting&quot;/&gt;都不会起作用,您需要在for-each之前添加&lt;xsl:variable name=&quot;context-node&quot; select=&quot;.&quot;/&gt;,然后在for-each内部使用&lt;xsl:value-of select=&quot;$context-node/@Reporting&quot;/&gt;

这可能仍然不够,因为您不太可能希望输出相同的内容 $Counter 次,但您尚未展示输入和所需输出,因此我无法确定您需要/想要进行的其他更改。

英文:

In XSLT 2 and later you can certainly use a 1 to $var expression e.g. &lt;xsl:for-each select=&quot;1 to xs:integer($Counter)&quot;&gt; 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. &lt;xsl:value-of select=&quot;@Reporting&quot;/&gt; will not work, you would need to put e.g. &lt;xsl:variable name=&quot;context-node&quot; select=&quot;.&quot;/&gt; before the for-each and then use e.g. &lt;xsl:value-of select=&quot;$context-node/@Reporting&quot;/&gt; 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.

huangapple
  • 本文由 发表于 2023年3月31日 20:05:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定