如何将 xmlns 命名空间添加到站点地图 xml 中的 urlset
How to add xmlns namespaces to urlset in sitemap xml
我有站点地图xml:
<urlset>
<url>
<loc>http://www.somedomain.com</loc>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
</urlset>
我正在使用 xslt
创建它,我需要 urlset
来拥有命名空间属性:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
如何添加它们?
这是我的 xslt
片段:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:element name = "urlset">
<!-- xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"-->
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
</xsl:call-template>
</xsl:element>
</xsl:template>
编辑
这是在@michael.hor257k 回答后编辑的片段:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<!-- update this variable on how deep your site map should be -->
<xsl:variable name="maxLevelForSitemap" select="6"/>
<xsl:template match="/">
<xsl:call-template name="urlset"> </xsl:call-template>
</xsl:template>
<xsl:template name="urlset">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<!-- more code here -->
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
</xsl:call-template>
</urlset>
</xsl:template>
<xsl:template name="drawNodes">
<xsl:param name="parent"/>
<xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
<xsl:for-each select="$parent/* [@isDoc and @level <= $maxLevelForSitemap and (umbracoNaviHide != '1' or not(umbracoNaviHide))]">
<xsl:if test="@id > 0">
<xsl:if test="not(umbracoRedirect) or umbracoRedirect = ''">
<xsl:element name="url">
<xsl:element name="loc">
<xsl:variable name="url" select="concat('http://',umbraco.library:RequestServerVariables('HTTP_HOST'))" />
<xsl:value-of select="concat($url,umbraco.library:NiceUrl(@id))" />
</xsl:element>
<xsl:element name="changefreq">
<xsl:if test="sitemapChange != ''">
<xsl:value-of select="sitemapChange" />
</xsl:if>
<xsl:if test="not(sitemapChange) or sitemapChange = ''">
<xsl:choose>
<xsl:when test="@level <= 2">weekly</xsl:when>
<xsl:when test="@level >= 3">monthly</xsl:when>
<xsl:when test="@level >= 4">weekly</xsl:when>
<xsl:otherwise>yearly</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:element>
<xsl:element name="priority">
<xsl:if test="not(sitemapChange) or sitemapChange = ''">
<xsl:choose>
<xsl:when test="@level <= 2">1</xsl:when>
<xsl:when test="@level >= 3">0.9</xsl:when>
<xsl:when test="@level >= 4">0.8</xsl:when>
<xsl:otherwise>0.7</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:if>
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
它仍然输出 url
和空的 xmlns 命名空间。
编辑 2
即使我在这里做一个简单的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="urlset" />
</xsl:template>
<xsl:template name="urlset">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<xsl:call-template name="url">
</xsl:call-template>
</urlset>
</xsl:template>
<xsl:template name="url">
<url>
</url>
</xsl:template>
</xsl:stylesheet>
我在 url
中仍然得到空命名空间:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:msxml="urn:schemas-microsoft-com:xslt">
<url xmlns="" />
</urlset>
这是不正确的。
你可以简单地做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/urlset">
<urlset
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<!-- more code here -->
</urlset>
</xsl:template>
</xsl:stylesheet>
然后在您的命名模板中,将所有 xsl:element
指令更改为文字结果元素 - 例如,而不是:
<xsl:element name="url">
<!-- more code here -->
</xsl:element>
做:
<url>
<!-- more code here -->
</url>
我有站点地图xml:
<urlset>
<url>
<loc>http://www.somedomain.com</loc>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
</urlset>
我正在使用 xslt
创建它,我需要 urlset
来拥有命名空间属性:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
如何添加它们?
这是我的 xslt
片段:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:element name = "urlset">
<!-- xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"-->
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
</xsl:call-template>
</xsl:element>
</xsl:template>
编辑
这是在@michael.hor257k 回答后编辑的片段:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<!-- update this variable on how deep your site map should be -->
<xsl:variable name="maxLevelForSitemap" select="6"/>
<xsl:template match="/">
<xsl:call-template name="urlset"> </xsl:call-template>
</xsl:template>
<xsl:template name="urlset">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<!-- more code here -->
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
</xsl:call-template>
</urlset>
</xsl:template>
<xsl:template name="drawNodes">
<xsl:param name="parent"/>
<xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
<xsl:for-each select="$parent/* [@isDoc and @level <= $maxLevelForSitemap and (umbracoNaviHide != '1' or not(umbracoNaviHide))]">
<xsl:if test="@id > 0">
<xsl:if test="not(umbracoRedirect) or umbracoRedirect = ''">
<xsl:element name="url">
<xsl:element name="loc">
<xsl:variable name="url" select="concat('http://',umbraco.library:RequestServerVariables('HTTP_HOST'))" />
<xsl:value-of select="concat($url,umbraco.library:NiceUrl(@id))" />
</xsl:element>
<xsl:element name="changefreq">
<xsl:if test="sitemapChange != ''">
<xsl:value-of select="sitemapChange" />
</xsl:if>
<xsl:if test="not(sitemapChange) or sitemapChange = ''">
<xsl:choose>
<xsl:when test="@level <= 2">weekly</xsl:when>
<xsl:when test="@level >= 3">monthly</xsl:when>
<xsl:when test="@level >= 4">weekly</xsl:when>
<xsl:otherwise>yearly</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:element>
<xsl:element name="priority">
<xsl:if test="not(sitemapChange) or sitemapChange = ''">
<xsl:choose>
<xsl:when test="@level <= 2">1</xsl:when>
<xsl:when test="@level >= 3">0.9</xsl:when>
<xsl:when test="@level >= 4">0.8</xsl:when>
<xsl:otherwise>0.7</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:if>
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
它仍然输出 url
和空的 xmlns 命名空间。
编辑 2
即使我在这里做一个简单的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="urlset" />
</xsl:template>
<xsl:template name="urlset">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<xsl:call-template name="url">
</xsl:call-template>
</urlset>
</xsl:template>
<xsl:template name="url">
<url>
</url>
</xsl:template>
</xsl:stylesheet>
我在 url
中仍然得到空命名空间:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:msxml="urn:schemas-microsoft-com:xslt">
<url xmlns="" />
</urlset>
这是不正确的。
你可以简单地做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/urlset">
<urlset
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<!-- more code here -->
</urlset>
</xsl:template>
</xsl:stylesheet>
然后在您的命名模板中,将所有 xsl:element
指令更改为文字结果元素 - 例如,而不是:
<xsl:element name="url">
<!-- more code here -->
</xsl:element>
做:
<url>
<!-- more code here -->
</url>