好吧,这真是一个艰难的时刻……我在生成不同宽度的表格单元格时遇到了困难。我正在使用 XML/XSLT 输出我的 HTML,所以宽度基本上以 XML 格式存储:
<size>
<width1>5</width1>
<width2>4</width2>
<width3>7</width3>
</size>
使用 XSLT 的属性集,我应该有一个表格行和宽度分别为 5px、4px、7px 的单元格。然而,问题在于 attribute-set
需要是 <xsl:stylesheet>
的 child 让它工作。我不能这样做:(原谅丢失的 px)
<tr>
<td>
<xsl:attribute-set name="style">
<xsl:attribute name="width"><xsl:value-of select="size/width1"/></xsl:attribute>
</xsl:attribute-set>
</td>
<td>
<xsl:attribute-set name="style">
<xsl:attribute name="width"><xsl:value-of select="size/width2"/></xsl:attribute>
</xsl:attribute-set>
</td>
<td>
<xsl:attribute-set name="style">
<xsl:attribute name="width"><xsl:value-of select="size/width3"/></xsl:attribute>
</xsl:attribute-set>
</td>
</tr>
有什么方法可以使用 XML 数据生成 html 标签来设置它们的样式吗?
最佳答案
您需要在 <td>
中添加 xsl:attribute 而不是 xsl:attribute-set元素:
<xsl:template match="size">
<tr>
<td>
<xsl:attribute name="width">
<xsl:value-of select="./width1"/>
</xsl:attribute>
</td>
<td>
<xsl:attribute name="width">
<xsl:value-of select="./width2"/>
</xsl:attribute>
</td>
<td>
<xsl:attribute name="width">
<xsl:value-of select="./width3"/>
</xsl:attribute>
</td>
</tr>
</xsl:template>
关于css - 使用 XSLT/XML 为 HTML 标签生成样式? (xsl :attribute-set),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090190/