所以我目前正在使用这个xslt样式表来为我的sitemap.xml提供一些样式。 sitemap.xml文件:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">
<url>
<loc>https://lakupon.com/</loc>
<changefreq>always</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://lakupon.com/tentang</loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
sitemap.xsl文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="urlset/url">
<tr>
<td><xsl:value-of select="loc" /></td>
<td><xsl:value-of select="priority" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出是这样的: sitemap.xml
有人可以告诉我有什么问题吗?谢谢!
答案 0 :(得分:0)
在您的XML中,您的元素具有默认命名空间“http://www.google.com/schemas/sitemap/0.9”
<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">
所以在你的XSL中,你必须首先用前缀声明命名空间(任何前缀都会这样做,我使用“ns”)。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://www.google.com/schemas/sitemap/0.9"
>
最后,在XPath语句中,您需要使用名称空间前缀(“ns”)
来引用节点<xsl:for-each select="ns:urlset/ns:url">
<tr>
<td><xsl:value-of select="ns:loc" /></td>
<td><xsl:value-of select="ns:priority" /></td>
</tr>
</xsl:for-each>
命名空间很荒谬,不要将它们添加到您自己的XML中,但这是在必要时处理它们的方法。