[xml与xslt] 中,如何获取缺省节点的name属性值?

auzhuang 2007-11-15 05:26:08
请教:
我使用了<xsl:template match="text()|@*"> <xsl:value-of select="."/></xsl:template> 来处理缺省的节点。但在其中,<xsl:value-of select="@name"/> 却无法获得name属性的值。 请教有什么方法?


test.xml
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="./test.xsl"?>
<article>
<title name="标题">hello</title>
<content name="内容">hello, world!</content>
<time name="时间">2007-10-01</time>
<url name="地址">www.csdn.net</url>
</article>


test.xsl
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
</head>
<body>
<table border="1">
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="title">
<tr>
<td class="col1"><xsl:value-of select="@name"/></td>
<td class="col2"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="content">
<tr>
<td class="col1"><xsl:value-of select="@name"/></td>
<td class="col2"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="text()|@*">
<tr>
<td class="col1"><xsl:value-of select="@name"/></td>
<td class="col2"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

</xsl:stylesheet>
...全文
885 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
这个简单啊,网上搜一下就得到答案了.
cyberimp 2008-02-13
  • 打赏
  • 举报
回复
也可以这样:

<xsl:template match="text( )|@*">
<tr>
<td class="col1">
<xsl:value-of select="../@name"/>
</td>
<td class="col2">
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
firetoucher 2007-12-17
  • 打赏
  • 举报
回复
<xsl:template match="text()|@*">

1 这个template节点url和time都进不来。缺省的rule会匹配它下面的text node,也就是文本,这是满足text() pattern。而text node没有属性的。所以
2 将该tempalte改为
<xsl:template match="/*/*">
<tr>
<td class="col1"><xsl:value-of select="@name"/></td>
<td class="col2"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

FT
--
Anything one man can imagine, other men can make real.
fcuandy 2007-11-16
  • 打赏
  • 举报
回复
    <xsl:template match="article/*">  
<tr>
<td class="col1"><xsl:value-of select="@name"/></td>
<td class="col2"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

8,907

社区成员

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

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