请问如何使用xsl:if判断一个节点内是否有内容?

屡败屡战数马援 2003-02-14 09:39:53
源文件是xml,格式如下:

<?xml version="1.0" encoding="gb2312"?>
<Root>
<Content>
......
</Content>
<ImgPath>
images/img.gif
</ImgPath>
</Root>

其中,<ImgPath>节点存放的是需要显示在网页的图片的内容,输出的xsl文件如下:

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html"/>
<!--以下显示content的内容 -->
<table width="596" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="596" height="404" valign="top">
<xsl:element name="textarea">
<xsl:attribute name="class">content</xsl:attribute>
<xsl:attribute name="readonly">true</xsl:attribute>
<xsl:value-of select="/Root/Content"/>
</xsl:element>
</td>
</tr>
</table>
</xsl:template>

<!-- 以下显示图片-->
<xsl:template match="ImgPath">
<xsl:element name="Img">
<xsl:attribute name="src">
<xsl:value-of select="/Root/ImgPath"/>
</xsl:attribute>
</xsl:element>
</xsl:template>

问题是源文件中的<ImgPath>中有的有内容,有的没有内容。我希望使用
xsl:if 判断<ImgPath>节点中的内容是否有,如果有就调用:
<xsl:template match="ImgPath">
的模板,请问这个xsl:if代码怎样写?

...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我明白了,关键是:
normalize-space(text()) != ''
的使用。
谢谢你,思归!!
saucer 2003-02-14
  • 打赏
  • 举报
回复
try

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html"/>
<xsl:template match="Root">
<table width="596" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="596" height="404" valign="top">
<xsl:element name="textarea">
<xsl:attribute name="class">content</xsl:attribute>
<xsl:attribute name="readonly">true</xsl:attribute>
<xsl:value-of select="Content"/>
</xsl:element>
</td>
</tr>
</table>
<xsl:apply-templates select="ImgPath" />
</xsl:template>

<!-- 以下显示图片-->
<xsl:template match="ImgPath">
<xsl:if test="normalize-space(text()) != ''">
<Img src="{normalize-space(text())}" />
</xsl:if>
</xsl:template>

</xsl:stylesheet>

8,906

社区成员

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

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