在test中,如何判断节点属性的值是否等于某个字符?

peppi 2002-10-06 04:47:11
有一XML:

<top>
<table>
<tr name="date">
<ITEM>
<text>654</text>
</ITEM>
</tr>
</table>
<table>
<tr name="logo">
<logo>
agd
</logo>
</tr>
</table>
</top>

我想根据<tr>的属性的不同值,做不同的XSLT处理
我的XSL:

<xsl:template match="top">
<body>
<xsl:choose>
<xsl:when test="string(table/tr/@name) = logo">
<xsl:apply-templates select="table" mode="logo"/>
</xsl:when>
<xsl:when test="string(table/tr/@name) = date">
<xsl:apply-templates select="table" mode="date"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="table" mode="others"/>
</xsl:otherwise>
</xsl:choose>
</body>
</xsl:template>

<xsl:template match="table" mode="logo">
<table width="97%" border="0" align="center" cellspacing="0"> <xsl:apply-templates select="tr" mode="logo"/>
</table>
</xsl:template>

<xsl:template match="table" mode="date">
<table width="100%" border="0" align="center" cellspacing="0">
<xsl:apply-templates select="tr" mode="date"/>
</table>
</xsl:template>

有错误,我估计问题在<xsl:when test="string(table/tr/@name) = logo">,test中该怎么判断某个节点的属性值是什么呢?我把string()去掉也不行,我换成table/tr/@name/text()也不行,换成test="table/tr[@name=logo]"也是不行,各位,我有两个要求:
一:给个正确答案
二:告诉我为什么以上那几个不行,问题在哪?帮我解解疑
谢谢!!!
...全文
195 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
outside 2002-10-09
  • 打赏
  • 举报
回复
<xsl:template match="top">中只对第一个<table>做处理


修改如下:
<xsl:template match="top">
<body>
<xsl:for-each select="table">
<xsl:if test="tr/@name= 'logo'">
<xsl:apply-templates select="tr" mode="logo"/>
</xsl:if>
<xsl:if test="tr/@name = 'date'">
<xsl:apply-templates select="tr" mode="date"/>
</xsl:if>
</xsl:for-each>
</body>
</xsl:template>


<xsl:template match="tr" mode="logo"> <!--处理LOGO-->
<table><tr><td>
<xsl:attribute name="bgcolor">
<xsl:value-of select="bgcolor"/>
</xsl:attribute>
<xsl:apply-templates select="logo|companypic"/>
</td>
</tr>
</table>
</xsl:template>

<xsl:template match="tr" mode="date"> <!--处理时间-->
<table>
<tr><td>
<xsl:attribute name="bgcolor">
<xsl:value-of select="'red'"/>
</xsl:attribute>
<xsl:attribute name="align">
<xsl:value-of select="location"/>
</xsl:attribute>
<xsl:apply-templates select="ITEM" mode="date"/>
</td></tr>
</table>
</xsl:template>
peppi 2002-10-07
  • 打赏
  • 举报
回复
您受累了~~~~~~

<xsl:template match="top">
<body>
<xsl:if test="string(table/tr/@name) = 'logo'">
<xsl:apply-templates select="table" mode="logo"/>
</xsl:if>
<xsl:if test="string(table/tr/@name) = 'date'">
<xsl:apply-templates select="table" mode="date"/>
</xsl:if>
</body>
</xsl:template>

<xsl:template match="table" mode="logo">
<table width="97%" border="0" align="center">
<xsl:apply-templates select="tr" mode="logo"/>
</table>
</xsl:template>

<xsl:template match="table" mode="date">
<table width="100%" border="0" align="center">
<xsl:apply-templates select="tr" mode="date"/>
</table>
</xsl:template>

<xsl:template match="tr" mode="logo"> <!--处理LOGO-->
<tr>
<xsl:attribute name="bgcolor">
<xsl:value-of select="bgcolor"/>
</xsl:attribute>
<xsl:apply-templates select="logo|companypic"/>
</tr>
</xsl:template>

<xsl:template match="tr" mode="date"> <!--处理时间-->
<tr>
<xsl:attribute name="bgcolor">
<xsl:value-of select="bgcolor"/>
</xsl:attribute>
<xsl:attribute name="align">
<xsl:value-of select="location"/>
</xsl:attribute>
<xsl:apply-templates select="ITEM" mode="date"/>
</tr>
</xsl:template>

在显示tr时,就会遇到我上面说的问题.很是奇怪.我不知道我哪里搞错了,请您指点!
孟子E章 2002-10-07
  • 打赏
  • 举报
回复
把你的xsl拿来
peppi 2002-10-06
  • 打赏
  • 举报
回复
孟先生:
谢谢指点.我现在把<xsl:when>换成<xsl:if>然后把<xsl:choose>去掉.我要根据@name的不同,输出不同的HTML,如XSL所写.可是我遇到一个问题,我用相应的XSL去单独解释任何一个table节点,都正常显示,但把他们加到一起,如果第一个XML的table和XSL中的第一个table模板对应,那就只显示它一个,后面的只有一个HTML中的table,而且属性是和前面的一样,但就没下文了.如果XML和XSL的table顺序不一样,它还不显示.这是怎么搞的?该怎么解决?才能让两个XML中的table都按XSL中的显示出来?
谢谢~
孟子E章 2002-10-06
  • 打赏
  • 举报
回复
<xsl:choose>
<xsl:when test="string(table/tr/@name) = 'logo'">
<xsl:apply-templates select="table" mode="logo"/>
</xsl:when>
<xsl:when test="string(table/tr/@name) = 'date'">
<xsl:apply-templates select="table" mode="date"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="table" mode="others"/>
</xsl:otherwise>
</xsl:choose>
孟子E章 2002-10-06
  • 打赏
  • 举报
回复
<xsl:when test="string(table/tr/@name) = 'logo'">,

8,906

社区成员

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

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