I have an XML document that has nodes in it that look like this:
<Variable name="var1" dataType="INT32"/>
<Variable name="var2" dataType="INT16"/>
<Variable name="var3" dataType="INT8"/>
I can loop over the variables and display the name and data type just fine, but I'd like to display the size of the variable, as well as the offset of it (first variable has an offset of zero, 2nd has an offset equal to the size of the first, 3rd has an offset equal to the size of the previous two). In the above example, var1 has a size of 4 and a offset of zero, var2 has a size of 2 and an offset of 4, var3 has a size of 1 and an offset of 6.
To print the size, this worked:
<xsl:variable name="fieldSize">
<xsl:choose>
<xsl:when test="contains(@dataType, 'INT8')">
<xsl:value-of select="'1'"/>
</xsl:when>
<xsl:when test="contains(@dataType, 'INT16')">
<xsl:value-of select="'2'"/>
</xsl:when>
<xsl:when test="contains(@dataType, 'INT32')">
<xsl:value-of select="'4'"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="'unknown'"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$fieldSize"/>
However, I have no idea how to print the offset! If field size was an attribute, I could do something like:
<xsl:variable name="offset" select="sum(preceding-sibling::Variable/@fieldSize)"/>
Since it's a variable and not an attribute, I can't do a sum over preceding-siblings to calculate offset. My next idea is to try to make an expression that can evaluate to the size based on the @dataType attribute, and maybe I can feed that into the "sum()" expression (no idea if that would work, though).
I attempted to create a NodeSet for fieldSizes, so I can lookup the size based on the attribute:
<xsl:variable name="fieldSizes">
<i ref="INT8">1</i>
<i ref="INT16">2</i>
<i ref="INT32">4</i>
</xsl:variable>
<xsl:value-of select="$fieldSizes[@ref=@dataType]"/>
However, the last line causes a Error during XSLT transformation: An XPath expression was expected to return a NodeSet. All of the below variants cause the same error:
<xsl:value-of select="$fieldSizes[@ref='INT8']"/>
<xsl:value-of select="$fieldSizes[@ref=INT8]"/>
<xsl:value-of select="$fieldSizes[1]"/>
How can I print the field size of the variable based on it's dataType? And once that works, how can I calculate the value of offset? Perhaps something like:
<xsl:variable name="offset" select="sum(preceding-sibling::Variable/$fieldSizes[@ref=@dataType])"/>