如何在xsl:copy时添加attribute

FiLng 2003-12-30 04:14:16
tmp.xml:

<tabel>
<tr height="20" >
<td class="1">wwwwwwwewref</td>
<td class="2">ssssssssssss</td>
<td class="3">sdfsdfsdfgfg</td>
</tr><tr height="20" >
<td class="1">wwwwwwwewref</td>
<td class="2">ssssssssssss</td>
<td class="3">sdfsdfsdfgfg</td>
</tr><tr height="20" >
<td class="1">wwwwwwwewref</td>
<td class="2">ssssssssssss</td>
<td class="3">sdfsdfsdfgfg</td>
</tr>
.........
</tabel>

我想处理上面的table,根据条件给不同的tr加上不同的attribute,比如给第一个添加style="tttt":

<tr height="20" style="tttt">
<td class="1">wwwwwwwewref</td>
<td class="2">ssssssssssss</td>
<td class="3">sdfsdfsdfgfg</td>
</tr>

如何写xslt?急!!
...全文
77 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
FiLng 2003-12-31
  • 打赏
  • 举报
回复
net_lover(孟子E章)

你的不对,
你的做法是对每一个重新生成<tr>
并添加attribute,如果原来的每个<tr>有多个不同的attribute,你的就不适用了!
saucer 2003-12-31
  • 打赏
  • 举报
回复
"tabel"? not "table"?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="tr">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:attribute name="style">color:red</xsl:attribute>
</xsl:when>

<xsl:when test="position() = 2">
<xsl:attribute name="style">color:green</xsl:attribute>
</xsl:when>

<xsl:otherwise>

</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
孟子E章 2003-12-30
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<tabel>
<xsl:for-each select="tabel/tr">
<tr>
<xsl:attribute name="height"><xsl:value-of select="@height"/></xsl:attribute>
<xsl:attribute name="style">style<xsl:value-of select="position()"/></xsl:attribute>
<xsl:copy-of select="*"/>
</tr>
</xsl:for-each>
</tabel>
</xsl:template>
</xsl:stylesheet>

8,907

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧