代码之家  ›  专栏  ›  技术社区  ›  Drifter64

首次使用XSLT:页面显示空白

  •  0
  • Drifter64  · 技术社区  · 12 年前

    我正在尝试使用此页面作为XSLT指南: http://www.w3schools.com/xml/xml_xsl.asp

    我基本上试图修改给定的XSL信息以适合我的标记,而这些标记恰好是站点地图的形式。然而,当我将XSL样式应用于XML页面时,它显示为空白!很明显,我错过了/忘记了或没有看到什么。请帮忙。

    我的XML(仅限代码段):

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="style.xsl" ?>
    <!-- sitemap-generator-url="http://www.auditmypc.com/free-sitemap-generator.asp" -->
    <!-- This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp -->
    <!-- Audit My PC also offers free security tools to help keep you safe during internet travels -->
    <urlset 
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
    http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
    >
    <url>
    <loc>http://www.site.com/</loc>
    <lastmod>2014-03-17T13:30:25-05:00</lastmod>
    </url>
    <url>
    <loc>http://www.site.com/index.php?format=feed&#x26;type=rss</loc>
    <lastmod>2014-03-17T13:28:20-05:00</lastmod>
    </url>
    </urlset>
    

    我的XSL:

    <?xml version="1.0" encoding="UTF-8"?>
    <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
    
    <xsl:for-each select="urlset/url">
    <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="loc"/></span>
    </div>
    
    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <span>Last Modified:<xsl:value-of select="lastmod"/></span>
    </div>
    </xsl:for-each>
    
    </body>
    </html>
    

    谢谢你的帮助!

    2 回复  |  直到 12 年前
        1
  •  1
  •   Ian Roberts    12 年前

    这个 urlset 元素及其后代都在示例XML文档中 http://www.sitemaps.org/schemas/sitemap/0.9 命名空间,因此为了将它们与XPath表达式匹配,必须将前缀绑定到该命名空间并一致使用:

    <?xml version="1.0" encoding="UTF-8"?>
    <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">
      <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
    
        <xsl:for-each select="sm:urlset/sm:url">
          <div style="background-color:teal;color:white;padding:4px">
            <span style="font-weight:bold"><xsl:value-of select="sm:loc"/></span>
          </div>
    
          <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
            <span>Last Modified:<xsl:value-of select="sm:lastmod"/></span>
          </div>
        </xsl:for-each>
    
      </body>
    </html>
    

    在XSLT/XXPath1.0中,非固定的元素名称 总是 表示没有命名空间中的元素,前缀名称从 样式表 ,而不是来自输入XML。

    顺便说一句,如果您正在编写XSLT以供浏览器在客户端解释,那么您将受限于XSLT1.0,因此我建议您找到一个很好的1.0特定教程和参考指南。w3schools上的资料并不总是清楚它指的是什么版本的XSLT/XXPath,如果你继续尝试不起作用的东西,却发现它们是2.0版本的构造,你会感到沮丧。。。

        2
  •  0
  •   keshlam    12 年前

    记住,XPath(因此XSLT)是命名空间感知的,但(至少在这些标准的1.0版本中)没有默认命名空间(xmlns=)赋值的概念。如果要选择名称空间节点,XPath必须使用绑定到正确名称空间的显式前缀。