【问题标题】:Error loading stylesheet: Parsing an XSLT stylesheet failed.?加载样式表时出错:解析 XSLT 样式表失败。?
【发布时间】:2017-09-02 03:11:51
【问题描述】:

当我尝试在 Firefox 中加载我的 xml 时收到此错误消息,当我尝试在 Safari 和 Google 中加载时收到空白页面。我错过了一个错误吗? 我的造型要求:

  • 年份---粗体
  • 标题----深蓝色字重900
  • 导演 - 深绿色,粗体
  • 演员 --- 男性为蓝色,女性为粉色
  • 类型----斜体和下划线
  • 时间----深红色,粗体,下划线

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="lab4_part2.xsl"?>
<streaming>
    <movies>
        <year>1985</year>
        <title>The Breakfast Club</title>
        <director>John Hughes</director>
        <actors>
            <male>Emilio Estevez</male>
            <male>Paul Gleason</male>
            <male>Anthony Micheal Hall</male>
            <male>Judd Nelson</male>
            <female>Molly Ringwald</female>
            <female>Ally Sheedy</female>
        </actors>
        <type>Comedy, Drama</type>
        <time>97</time>
    </movies>
    <movies>
        <year>1994</year>
        <title>Forrest Gump</title>
        <director>Robert Zemeckis</director>
        <actors>
            <male>Tom Hanks</male>
            <male>Mykelti Williamson</male>
            <male>Gary Sinise</male>
            <male>Haley Joel Osment</male>
            <female>Sally Field</female>
            <female>Robin Wright</female>
        </actors>
        <type>Comedy, Drama, Romance</type>
        <time>142</time>
    </movies>
</streaming>

XSLT:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="streaming/movies">
          <div>
            <p><xsl:value-of select="year" style="font-weight:bold;"/></p>
            <p><xsl:value-of select="title" style="color:DarkBlue; font-
weight:900;"/></p>
            <p><xsl:value-of select="director" style="color:DarkGreen; font-
weight:bold;"/></p>
            <p>Actors</p>
            <ul>
              <xsl:for-each select="streaming/movies/actors">
                <xsl:if test="name() = 'male'">
                  <li><xsl:value-of select="male" style="color:blue;"/></li>
                </xsl:if>
                <xsl:if test="name() = 'female'">
                  <li><xsl:value-of select="male" style="color:pink;"/></li>
                </xsl:if>
              </xsl:for-each>
            </ul>
            <p><xsl:value-of select="type" style="text-decoration:underline; 
font-style:italic;"/></p>
            <p><xsl:value-of select="time" style="color:DarkRed; text-
decoration:underline; font-weight:bold;"/></p>
          </div>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 您已被告知错误是什么,但问题的根本原因是您正在运行此样式表而没有看到错误消息。通常,在浏览器中运行 XSLT 并不是最好的开发环境,但大多数浏览器确实会在开发者控制台中提供诊断信息,您需要熟悉如何找到它们。
  • 我还推荐了xsltransform.net 作为测试 XSLT 的工具,但不幸的是,该服务目前不可用...
  • @TimC 我真的认为你应该发布一个规范的答案。该服务现已备份。

标签: html xml xslt transform styling


【解决方案1】:

你所有的xsl:value-of 语句似乎都有style 属性

<p>
   <xsl:value-of select="year" style="font-weight:bold;"/>
</p>

style 不是xsl:value-of 的有效属性。在所有情况下,您都应该将 style 属性移动到您正在创建的 html 标记上。例如

<p style="font-weight:bold;">
   <xsl:value-of select="year" />
</p>

【讨论】:

  • 哇,你说的太对了。愚蠢的错误。但是,我的 ul 演员仍然无法正确显示。
  • 这是一个单独的问题。但这是因为你在做&lt;xsl:for-each select="streaming/movies/actors"&gt;。此时你的上下文已经在movies 节点上,所以你需要做的就是&lt;xsl:for-each select="actors"&gt;
  • 其实需要&lt;xsl:for-each select="actors/*" /&gt;得到actors下的子节点,然后&lt;xsl:value-of select="." /&gt;才能取出子节点​​的实际文本值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多