2
我使用XSLT做HTML到XML的轉換,輸入HTML我有這樣的表,使用XSLTXSLT - 數字驗證
<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>
我需要生成以下輸出,
<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>
要做這個任務我寫下面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>
這給了我正確的輸出結構,但問題是當一些nu mber值是空的,它顯示了NaN
的屬性。
我的輸出如下,
<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>
我們如何處理這種數量驗證的XSLT並返回0而不是NaN
,並得到正確的輸出數值?