Creating a Local Variable within a for-each fo:table-row

53 Views Asked by At

I need to make create local variables within a fo:table-body to filter for-each created fo:table-rows. I cannot insert an fo:block as a child of the table-body. Is there any other way I make these Variables 'local' besides using a Block and which would be compatible with the fo:table-body's permitted children? Any help very much appreciated!

I had started by inserting the Variables as children of the xsl:for-each, within which I generate the fo:table-row (see below). I hoped the variables would remain local within each row but the absence of an fo:block means these variables are being applied globally (at the table level).

I need the variables to be local to each $trajectoryPoint to apply to subsequent for-each filtering.

I also tried using a template but the param's are treated the same way, I guess due to the absence of a Block between table-body and the for-each.

<fo:table-body>
    <xsl:for-each select="$trajectoryPoint">
    <xsl:variable name="dist1" select="./distance1"/>
    <xsl:variable name="dist2" select="./distance2"/>
        <xsl:table-row>
             <fo:table-cell>
                 <fo:block>
                     <xsl:text>REGULAR ROW INFO</xsl:text>
                 </fo:block>
              </fo:table-cell>
        </fo:table-row>
    <xsl:for-each select="otherNodeSet[/Distance &gt;= $dist1 and /Distance &lt; $dist2]">
        <xsl:table-row>
            <fo:table-cell>
                <fo:block>
                    <xsl:text>SPECIAL ROW INFO</xsl:text>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:for-each>
    </xsl:for-each>
</fo:table-body>
1

There are 1 best solutions below

2
Tony Graham On

Change:

<xsl:for-each select="$trajectoryPoint">
    <xsl:variable name="dist1" select="$trajectoryPoint/distance1"/>
    <xsl:variable name="dist2" select="$trajectoryPoint/distance2"/>

to:

<xsl:for-each select="$trajectoryPoint">
    <xsl:variable name="dist1" select="./distance1"/>
    <xsl:variable name="dist2" select="./distance2"/>

When $trajectoryPoint contains multiple nodes, the xsl:for-each processes each of them separately, but each time, you are setting $dist1 and $dist2 to all of the distance1 children and distance2 children, respectively, of all of the nodes in $trajectoryPoint.

Each time that xsl:for-each processes an item, it sets the context item (. in XPath) to that item. (See https://www.w3.org/TR/xslt-30/#element-for-each) Using . instead of $trajectoryPoint gets you the distance1 and distance2 children of just the context item instead of all of them.