2010-10-01 7 views
12

मेरा Google साइटमैप xmlns = "http: //www.sitemaps के बिना XSLT ठीक के माध्यम से अच्छी तरह से प्रस्तुत करता है। < urlset> तत्व में संगठन/स्कीमा/साइटमैप/0.9 ", हालांकि, जब मेरा foreach कथन काम नहीं करता है और टेम्पलेट में कुछ भी प्रस्तुत नहीं करता है। मेरा कोड नीचे है। आपकी सहायता के लिए धन्यवाद.एक्सएसएलटी काम नहीं करता है जब मैं xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"

एक्सएमएल

<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

अच्छा प्रश्न (+1)। एक स्पष्टीकरण के लिए और एक पूर्ण समाधान के लिए मेरा जवाब देखें। –

उत्तर

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

यह एक सामान्य प्रश्न है।

XPath किसी भी अपरिपक्व नाम को "नो नेमस्पेस" से संबंधित मानता है। हालांकि, प्रदत्त दस्तावेज़ में तत्व "http://www.sitemaps.org/schemas/sitemap/0.9" नामस्थान से संबंधित हैं - "no namespace" पर नहीं।

इसलिए, निम्नलिखित XPath अभिव्यक्ति किसी भी नोड का चयन नहीं करता:

urlset/url 

समाधान:

XSLT स्टाइलशीट में "http://www.sitemaps.org/schemas/sitemap/0.9" नाम स्थान परिभाषित करें और इसे करने के लिए एक उपसर्ग संबद्ध करते हैं। फिर किसी भी 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> 

जब इस बदलाव प्रदान की XML दस्तावेज़ पर लागू किया जाता है:

<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

उत्कृष्ट जवाब! धन्यवाद! आपकी व्याख्या बहुत स्पष्ट थी और समाधान काम किया! – Julian

+0

क्या आप जानते हैं कि {site_url} को एक क्लिक करने योग्य यूआरएल में कैसे बनाया जाए। जब मैं नीचे दिए गए कोड को आज़माता हूं, तो मुझे निम्न त्रुटि मिलती है - "एक्सएमएल पार्सिंग त्रुटि: अच्छी तरह से गठित नहीं स्थान: sitename।कॉम/साइटमैपएक्सएसएल लाइन नंबर 1 9 4, कॉलम 26: "एक्सएसएल सिंटैक्स के पहले बाएं ब्रेस के लिए एक तीर बिंदु के साथ - कोड निम्नानुसार है -" " – Julian

+1

@ जूलियन: यह एक और अकसर किये गए सवाल है :)। << a href = '{s: loc}'> ' –

3

xpath के रूप में नाम स्थान की आवश्यकता होगी:

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

इसे सही ढंग से निम्न परिणाम का उत्पादन एक उपसर्ग, उदाहरण के लिए

{http://www.sitemaps.org/schemas/sitemap/0.9}urlset 

अगर यह xmlns था: एक्स = "http://www.sitemaps.org/schemas/sitemap/0.9" आप इस्तेमाल कर सकते हैं

x:urlset 

यह इस पेज की तरह दिखता है में मदद मिलेगी http://msdn.microsoft.com/en-us/library/ms950779.aspx

संपादित करें: मैं इसे पोस्ट करने जा रहा था और उपसर्ग को परिभाषित करने के लिए xsl का उपयोग करने के उदाहरण के साथ अनुवर्ती हूं, लेकिन डिमिट्रे पहले से ही है।

संबंधित मुद्दे