2010-10-01 9 views
12

Moja mapa witryny Google renderuje dobrze XSLT bez Xmlns = "http: //www.sitemaps. org/schemas/sitemap/0.9 "w elemencie urlset> <, jednak jeśli jest zawarte, moje oświadczenie foreach nie działa i nic nie renderuje w szablonie. Mój kod jest poniżej. Dzięki za pomoc.XSLT nie działa, gdy dołączam xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"

XML

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
<url> 
<loc>{site_url}</loc> 
<lastmod>{current_time format="%Y-%m-%d"}</lastmod> 
<changefreq>monthly</changefreq> 
<priority>0.5</priority> 
</url> 
</urlset> 

XSL

<xsl:template match="/"> 
<html> 
<body> 
<h2>Sitemap</h2> 
<table border="1"> 
<tr bgcolor="#9acd32"> 
    <th>Location</th> 
    <th>Last Modified</th> 
    <th>Update Frequency</th> 
    <th>Priority</th> 
</tr> 
<xsl:for-each select="urlset/url"> 
<tr> 
    <td><xsl:value-of select="loc"/></td> 
    <td><xsl:value-of select="lastmod"/></td> 
    <td><xsl:value-of select="changefreq"/></td> 
    <td><xsl:value-of select="priority"/></td> 
</tr> 
</xsl:for-each> 
</table> 
</body> 
</html> 
+0

Dobre pytanie (+1). Zobacz moją odpowiedź na wyjaśnienie i pełne rozwiązanie. –

Odpowiedz

17

My Google sitemap renders well through XSLT fine without the xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" in the <urlset> element, however when included, my foreach statement doesn't work and nothing renders in the template

To FAQ.

XPath traktuje każdą nieprefiksowaną nazwę jako należącą do "bez przestrzeni nazw". Jednak elementy w dostarczonym dokumencie należą do przestrzeni nazw "http://www.sitemaps.org/schemas/sitemap/0.9" - a nie do "no namespace".

Dlatego następujące wyrażenie XPath nie wybrać dowolny węzeł w ogóle:

urlset/url 

Rozwiązanie:

Zdefiniuj "http://www.sitemaps.org/schemas/sitemap/0.9" nazw w arkuszu stylów XSLT i skojarzyć prefiks do niego. Następnie użyj tego przedrostka ze wszystkimi nazwami, które biorą udział w dowolnym wyrażeniu XPath.

<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" 
> 

<xsl:template match="/"> 
    <html> 
    <body> 
     <h2>Sitemap</h2> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th>Location</th> 
      <th>Last Modified</th> 
      <th>Update Frequency</th> 
      <th>Priority</th> 
     </tr> 
     <xsl:for-each select="s:urlset/s:url"> 
      <tr> 
      <td><xsl:value-of select="s:loc"/></td> 
      <td><xsl:value-of select="s:lastmod"/></td> 
      <td><xsl:value-of select="s:changefreq"/></td> 
      <td><xsl:value-of select="s:priority"/></td> 
      </tr> 
     </xsl:for-each> 
     </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

kiedy ta transformacja jest stosowane na dostarczonym dokumencie XML:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
    <url> 
     <loc>{site_url}</loc> 
     <lastmod>{current_time format="%Y-%m-%d"}</lastmod> 
     <changefreq>monthly</changefreq> 
     <priority>0.5</priority> 
    </url> 
</urlset> 

poprawnie generuje następujący wynik:

<html> 
    <body> 
     <h2>Sitemap</h2> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th>Location</th> 
      <th>Last Modified</th> 
      <th>Update Frequency</th> 
      <th>Priority</th> 
     </tr> 
     <tr> 
      <td>{site_url}</td> 
      <td>{current_time format="%Y-%m-%d"}</td> 
      <td>monthly</td> 
      <td>0.5</td> 
     </tr> 
     </table> 
    </body> 
</html> 
+0

Doskonała odpowiedź! Dzięki! Twoje wyjaśnienie było bardzo jasne i rozwiązanie zadziałało! – Julian

+0

Czy wiesz, jak zrobić {site_url} w klikalnym adresie URL. Kiedy próbuję poniższy kod, pojawia się następujący błąd - "Błąd analizy XML: źle sformułowana lokalizacja: nazwa_strony.com/sitemapxsl Numer wiersza 194, Kolumna 26: "ze strzałką wskaż pierwszą lewą klamrę składni xsl - Kod jak poniżej -" " – Julian

+1

@Julian: To jest kolejne FAQ :). Użyj:' ' –