| « | October 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:1 评论数量:0 留言数量:0 访问次数:14488 建立时间:2010年5月21日 |

| |
|
如何在XSLT样式表中声明命名空间 文章收藏, 网上资源, 软件技术, 电脑与网络
xnotepad 发表于 2010/7/14 12:40:16 |
| 考虑以下两段代码: <urlset> <url> <loc>http://www.caixw.com/archives/1.html</loc> <lastmod>2010-05-20T16:30:59+08:00</lastmod> </url> <url> <loc>http://www.caixw.com/archives/2.html</loc> <lastmod>2010-05-20T16:35:59+08:00</lastmod> </url></urlset> 这是一段标准的sitemap文件,当然为简单点,我把url的两个子节点去掉了,但总体上不影响我们使用。 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/Transform"><xsl:output method="html" encoding="utf-8" indent="yes" version="1.0" /><xsl:template match="/" xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9"><html> <head> <title>XML Sitemap</title> </head> <body> <xsl:apply-templates select="urlset" /> </body></html></xsl:template><xsl:template match="urlset"><table> <xsl:for-each select="url"> <tr> <td><a> <xsl:attribute name="href"><xsl:value-of select="loc" /></xsl:attribute> <xsl:value-of select="loc" /> </a></td> <td><xsl:value-of select="substring-before(lastmod, 'T')" /></td> </tr> </xsl:for-each></table></xsl:template></xsl:stylesheet>& lt;/ol> 这是一段转换sitemap的代码,再次偷懒,把输出的表格的内容给过滤掉了一部分。要完整版的看本站右下角的sitemap文件。 以上这两段代码能够正确的进行转换,没有什么问题。现在我们给xml文件加上一个命名空间(事实上现在大部分XML文件都存在这样那样的命名空间,sitemap也不例外): <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <!-- ... --> </url></urlset> OK,现在应该不能正确转换了,body部分变成空的了。很明显无法匹配urlset节点了。因为现在所有sitemap文件下的节点都是带命名空间的,不能再这样直接使用了。都要带上命名空间前缀才能正确匹配。 当然,要使用命名空间,首先要声明它。在xsl:stylesheet标签中再声明一个命名空间: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/Transform" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"> 然后在所有原先匹配XML文件节点的都加上"sitemap"前缀: <td><xsl:value-of select="substring-before(sitemap:lastmod, 'T')" />
原文在这里:http://www.caixw.com/archives/how-to-declare-namespace-in-xslt-stylesheet.html |
|
|