Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>My Google sitemap renders well through XSLT fine without the <code>xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"</code> in the <code>&lt;urlset&gt;</code> element, however when included, my foreach statement doesn't work and nothing renders in the template</p> </blockquote> <p><strong>This is a FAQ</strong>.</p> <p>XPath treats any unprefixed name as belonging to "no namespace". However, the elements in the provided document belong to the <code>"http://www.sitemaps.org/schemas/sitemap/0.9"</code> namespace -- not to <code>"no namespace"</code>.</p> <p>Therefore, the following XPath expression doesn't select any node at all:</p> <pre><code>urlset/url </code></pre> <p><strong>Solution</strong>:</p> <p>Define the <code>"http://www.sitemaps.org/schemas/sitemap/0.9"</code> namespace in the XSLT stylesheet and associate a prefix to it. Then use this prefix with all names that participate in any XPath expression.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://www.sitemaps.org/schemas/sitemap/0.9" exclude-result-prefixes="s" &gt; &lt;xsl:template match="/"&gt; &lt;html&gt; &lt;body&gt; &lt;h2&gt;Sitemap&lt;/h2&gt; &lt;table border="1"&gt; &lt;tr bgcolor="#9acd32"&gt; &lt;th&gt;Location&lt;/th&gt; &lt;th&gt;Last Modified&lt;/th&gt; &lt;th&gt;Update Frequency&lt;/th&gt; &lt;th&gt;Priority&lt;/th&gt; &lt;/tr&gt; &lt;xsl:for-each select="s:urlset/s:url"&gt; &lt;tr&gt; &lt;td&gt;&lt;xsl:value-of select="s:loc"/&gt;&lt;/td&gt; &lt;td&gt;&lt;xsl:value-of select="s:lastmod"/&gt;&lt;/td&gt; &lt;td&gt;&lt;xsl:value-of select="s:changefreq"/&gt;&lt;/td&gt; &lt;td&gt;&lt;xsl:value-of select="s:priority"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/xsl:for-each&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the provided XML document</strong>:</p> <pre><code>&lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt; &lt;url&gt; &lt;loc&gt;{site_url}&lt;/loc&gt; &lt;lastmod&gt;{current_time format="%Y-%m-%d"}&lt;/lastmod&gt; &lt;changefreq&gt;monthly&lt;/changefreq&gt; &lt;priority&gt;0.5&lt;/priority&gt; &lt;/url&gt; &lt;/urlset&gt; </code></pre> <p><strong>it correctly produces the following result</strong>:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;h2&gt;Sitemap&lt;/h2&gt; &lt;table border="1"&gt; &lt;tr bgcolor="#9acd32"&gt; &lt;th&gt;Location&lt;/th&gt; &lt;th&gt;Last Modified&lt;/th&gt; &lt;th&gt;Update Frequency&lt;/th&gt; &lt;th&gt;Priority&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;{site_url}&lt;/td&gt; &lt;td&gt;{current_time format="%Y-%m-%d"}&lt;/td&gt; &lt;td&gt;monthly&lt;/td&gt; &lt;td&gt;0.5&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload