Reputation: 1020
I'm doing html to xml transformation using xslt, input html I have tables like this,
<doc>
<table>
<tbody>
<tr>
<td rowspan="3">aaa</td>
<td colspan="5">bbb</td>
<td rowspan="3">ccc</td>
<td colspan="2">ddd</td>
</tr>
</tbody>
</table>
</doc>
using xslt I need to generate following output,
<doc>
<table>
<tbody>
<tr>
<entry namest="1" morerows="2">aaa</entry>
<entry namest="2" nameend="6">bbb</entry>
<entry namest="7" morerows="2">ccc</entry>
<entry namest="8" nameend="9">ddd</entry>
</tr>
</tbody>
</table>
</doc>
To do this task I have written following xsl
<xsl:template match="td">
<xsl:variable name="pre_rowspan" select="count(preceding-sibling::td[@rowspan])+1"/>
<xsl:variable name="pre_colspan" select="preceding-sibling::td[@colspan]/@colspan"/>
<xsl:variable name="numberof_pre_rowspan" select="count(preceding-sibling::td[@rowspan])+1"/>
<entry>
<xsl:attribute name="namest" select="number($pre_rowspan + $pre_colspan)"/>
<xsl:if test="@rowspan">
<xsl:attribute name="morerows" select="number(@rowspan)-1"/>
</xsl:if>
<xsl:if test="@colspan">
<xsl:attribute name="nameend"
select="number(@colspan)+count(preceding-sibling::td[@rowspan])+number(preceding-sibling::td[@colspan]/@colspan)"
/>
</xsl:if>
<xsl:apply-templates/>
</entry>
</xsl:template>
This gives me correct structure of the output but the problem is when some number values are empty it shows NaN
of the attributes.
My output is follows,
<doc>
<table>
<tbody>
<tr>
<entry namest="NaN" morerows="2">aaa</entry>
<entry namest="NaN" nameend="NaN">bbb</entry>
<entry namest="7" morerows="2">ccc</entry>
<entry namest="8" nameend="9">ddd</entry>
</tr>
</tbody>
</table>
</doc>
How can we handle this kind of number validations in XSLT and return 0 instead of NaN
and get the correct output number values?
Upvotes: 2
Views: 420
Reputation: 8803
I recommend to you to use a decimal-format
to specify a NaN mask:
<xsl:decimal-format name = "myformat" NaN = "0"/>
<xsl:template match="td">
...
<xsl:attribute name="namest" select="format-number(number($pre_rowspan + $pre_colspan), '#0', 'myformat')"/>
...
</xsl:template>
Upvotes: 2