I'm trying to make a XSLT conversion that generates C code, the following XML should be converted:
<enum name="anenum">
<enumValue name="a"/>
<enumValue name="b"/>
<enumValue name="c" data="10"/>
<enumValue name="d" />
<enumValue name="e" />
</enum>
It should convert to some C code as following:
enum anenum {
a = 0,
b = 1,
c = 10,
d = 11,
e = 12
}
or alternatively (as the C preprocessor will handle the summation):
enum anenum {
a = 0,
b = 1,
c = 10,
d = c+1,
e = c+2
}
The core of my XSLT looks like:
<xsl:for-each select="enumValue">
<xsl:value-of select="name"/>
<xsl:text> = </xsl:text>
<xsl:choose>
<xsl:when test="string-length(@data)>0">
<xsl:value-of select="@data"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="position()-1"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>,
(for simplicity I skip some of the 'no comma at the last element' code)
This example will not generate the correct values for d and e
I've been trying to get it working for the variable d and e, but so far I'm unsuccessful.
Using constructions like:
<xsl:when test="string-length(preceding-sibling::enumValue[1]/@datavalue)>0">
<xsl:value-of select="preceding-sibling::enumValue/@data + 1"/>
</xsl:when>
...only work for the first one after the specified value (in this case d).
Who can help me? I'm probably thinking too much in a procedural way...