所以我目前正在使用此 xslt 样式表为我的 sitemap.xml 提供一些样式。 站点地图.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>
站点地图.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
谁能告诉我哪里出了问题??谢谢!
最佳答案
在您的 XML 中,您的元素具有默认命名空间“http://www.google.com/schemas/sitemap/0.9”
<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">
因此,在您的 XSL 中,您必须首先使用前缀声明 namespace (任何前缀都可以,我使用“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 中,但这是在您必须时处理它们的方法。
关于css - XML - 使用 xslt 样式表,没有给出输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089704/