在XSL循环中使用Variable来保存动态值

时间:2018-04-20 09:54:04

标签: xslt

使用xsl,我在循环中查找变量以重置新记录。

从oracle表中获取记录

<xsl:variable name="curr_temp_emp_no" select="'##'"/>
<xsl:for-each select="/data/test/loof/super_incompleted">
<xsl:variable name="curr_temp_emp_no2" select="emp_no"/>
<xsl:choose>
<xsl:when test="$curr_temp_emp_no2 != $curr_temp_emp_no">
<xsl:value-of select="emp_no"/><fo:inline> - </fo:inline><xsl:value-of select="variable_desc"/></xsl:when>

<xsl:otherwise>
<xsl:value-of select="variable_desc"/>
</xsl:otherwise>
</xsl:choose>   
<xsl:variable name="curr_temp_emp_no" select="emp_no"/>
</xsl:for-each>

我正在尝试比较变量&#34; curr_temp_emp_no&#34;如果新值只打印&#34; emp_no - variable_desc&#34;否则(如果相同的emp_no)然后只打印&#34; variable_desc&#34;。

我从谷歌了解XSLT中的变量是不可变的,一旦我们为它们赋值,我们就无法改变它们。

Refence:Can you simulate a boolean flag in XSLT?

任何人都可以帮我写这个逻辑。

3 个答案:

答案 0 :(得分:1)

您要求我们从非工作代码中反向设计您的需求,这始终是一项挑战,但您似乎正在从过程编程语言中汲取创意,这为我们提供了一些关于您想要的内容的线索代码要做。

基本上它看起来像是一个“群体相邻”的问题。在XSLT 2.0中你可以做到

<xsl:for-each-group select="/data/test/loof/super_incompleted" 
                    group-adjacent="emp_no">
  ...
</xsl:for-each-group>

如果您坚持使用XSLT 1.0,那么通常的方法是使用递归命名模板而不是for-each指令处理节点序列:将节点列表作为参数传递给模板,以及当前的员工ID,然后在模板中处理列表中的第一个节点,并递归调用自己来处理列表的其余部分。

@ChristianMosz已将此建议扩展为工作代码。

答案 1 :(得分:1)

看起来你想要比较最后两个值。我可以通过以下方式实现这一目标:

<xsl:template match="/">
    <xsl:call-template name="helperTemplate">
        <xsl:with-param name="nodes" select="/data/test/loof/super_incompleted"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="helperTemplate">
    <xsl:param name="nodes"/>
    <xsl:param name="oldVal" select="''"/>

    <xsl:if test="$nodes">
        <xsl:variable name="curr_temp_emp_no" select="$nodes[1]/emp_no"/>
        <xsl:choose>
            <xsl:when test="$curr_temp_emp_no != $oldVal">
                <xsl:value-of select="$curr_temp_emp_no"/>
                <fo:inline> - </fo:inline>
                <xsl:value-of select="$nodes[1]/variable_desc"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$nodes[1]/variable_desc"/>
            </xsl:otherwise>
        </xsl:choose>

        <xsl:call-template name="helperTemplate">
            <xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
            <xsl:with-param name="oldVal" select="$nodes[1]/emp_no"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

如果您想对所有值进行比较(如果之前没有该元素的元素),您可以先对它们进行排序并使用相同的代码。

答案 2 :(得分:0)

感谢您的回复。 我使用了&#34;前兄弟::&#34;这解决了我的问题。现在代码是这样的。

<xsl:for-each select="/data/test/loof/super_incompleted">
<xsl:variable name="curr_temp_emp_no2" select="emp_no"/>
<xsl:choose>
<xsl:when test="$curr_temp_emp_no2 != preceding-sibling::super_incompleted[1]/emp_no">
<xsl:value-of select="emp_no"/><fo:inline> - </fo:inline><xsl:value-of select="variable_desc"/></xsl:when>

<xsl:otherwise>
<xsl:value-of select="variable_desc"/>
</xsl:otherwise>
</xsl:choose>   
<xsl:variable name="curr_temp_emp_no" select="emp_no"/>
</xsl:for-each>

之前的表格就像这样。 1- ABC,1- DFE,1- GFH 2- DFG,2- FGH,2- SDS,2- RTY

现在表格看起来像这样。 1- ABC,DFE,GFH 2- DFG,FGH,SDS

相关问题