sitemap.xml设置样式

作者: dreamfly 分类: seo 发布时间: 2019-04-05 01:51

我们都知道html可以通过css来设置样式,那么xml可以设置样式吗?它是通过css来设置样式的吗?

xml主要是通过xsl来设置样式的,首先我们需要在xml引入样式表,也就是xsl文件。

<?xml-stylesheet type='text/xsl' href='/url/to/xsl-stylesheet.xsl'?>

例如上面这样,href对应xsl的路径,在xsl里面我们可以定义显示的xml结构和样式,其中样式通过css来控制。

css->html  xsl->xml ,在xsl中,我们不仅可以控制样式,还可以控制结构,我们可以对显示内容进行格式化。

<xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
            <tr><xsl:if test="position() mod 2 != 1"><xsl:attribute  name="class">high</xsl:attribute></xsl:if>
                <td><xsl:value-of select="position()"/></td>
                <td><xsl:variable name="itemURL"><xsl:value-of select="sitemap:loc"/></xsl:variable>
                    <a href="{$itemURL}"><xsl:value-of select="sitemap:loc"/></a>
                </td>
                <td><xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/></td>
            </tr>
</xsl:for-each>

我们通过for-each 换循环xml中的节点,xsl:value-of 用来获取xml中节点属性 select可以用来做一些复杂的截取,拼接操作。

如果提示This XML file does not appear to have any style information associated with it. The document tree is shown below.

那就是没有引入xml-stylesheet

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!