Search code examples
htmlxsltxhtmlxslt-1.0xls

Why i always get first item from array-variable?


$headerTitles has 4 values, but i always receive value with "1" index... Why?

`<xsl:variable name="tgroup" select="../../.."/>
    <xsl:variable name="colspecs" select="$tgroup/colspec"/>
    <xsl:variable name="headerTitles" select="$tgroup/thead/row/entry"/>

    <xsl:variable name="columnNumber">
      <xsl:call-template name="entry.getColspecAttributeValue">
        <xsl:with-param name="colspecs" select="$colspecs" />
        <xsl:with-param name="attrName">colNum</xsl:with-param>
        <xsl:with-param name="isLastEmpty">false</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    <xsl:attribute name="columnName">
      **<xsl:value-of select="$headerTitles[$columnNumber]"/>**
    </xsl:attribute>`

$headerTitles has 4 values, but i always receive value with "1" index... Why?


Solution

  • -- edited in response to comments --

    $headerTitles has 4 values, but i always receive value with "1" index... Why?

    If the instruction:

    <xsl:value-of select="$headerTitles[3]"/>
    

    returns the string-value of the 3rd entry, but:

    <xsl:value-of select="$headerTitles[$columnNumber]"/>
    

    returns the value from the 1st entry, then the $columnNumber variable does not contain the number 3. Instead, it contains some value that when evaluated as a Boolean returns true for all entries (this could be even the string "3").

    In such situation, the xsl:value-of instruction in XSLT 1.0 will return the string-value of the first node in the selected node-set - see:
    https://www.w3.org/TR/1999/REC-xslt-19991116/#value-of
    https://www.w3.org/TR/1999/REC-xpath-19991116/#section-String-Functions