【问题标题】:XSLT not displaying at allXSLT 根本不显示
【发布时间】:2018-05-10 03:20:40
【问题描述】:

我是使用 xml 和 xslt 的绝对初学者。

我有一个 xml 文档和一个 xslt 文档,但由于某种原因,我无法让它在浏览器(Internet Explorer)中正确显示。

如果您能深入了解为什么会发生这种情况,我们将不胜感激。它与命名空间有关吗?我有一个命名空间,但我看到的示例实际上并未在其 xml 文档中包含命名空间。这是我能想到的唯一会引起问题的事情。

谢谢。

这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="D:\menu.xsl" ?>
<menu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.wasabi.com/pakistan/menu"
    xsi:schemaLocation="http://www.wasabi.com/pakistan/menu file:///D:/XML/schemas/menuSchema.xsd" > <!--Tells the document where to find the associated schema-->

<menuItem>
    <name>chicken</name>
    <price>$9.99</price>
    <image> 
        <img src="chiccken.png"/>
    </image>
    <description>
    <!--Just some flavor text -->
       <![CDATA[ 
                 hello there blah. 
                ]]>
    </description>
    <calories>
        <calorieCount> 1200 </calorieCount> <!--set as integer -->
    </calories>
    <icon/>
</menuItem>

<menuItem>
    <name>turkey</name>
    <price>$4.99</price>
    <image> 
        <img src="turkey.png"/>
    </image>
    <description>
       <![CDATA[ 
                 lorem ipsum ...
                ]]>
    </description>
    <calories>
        <calorieCount> 500 </calorieCount>
    </calories>
    <icon iconName="Sodium">&#9830;</icon><!--Character reference that tells customer that means the dish is low in sodium -->
</menuItem> 

这是我的 XSL:

<?xml version="1.0"?>

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

<xsl:template match="/">
    <html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

            <script>
            $(document).ready(function(){
                document.title = 
(location.pathname.substring(location.pathname.lastIndexOf("/") + 
1)).replace('.xml','');

            });
            </script>



            <title> </title>


        </head>

        <body>

             <xsl:apply-templates/>  

        </body>

    </html>
</xsl:template>

<xsl:template match="menuItem">
  <p>
    <xsl:apply-templates select="name"/>  
    <xsl:apply-templates select="price"/>
  </p>
</xsl:template>

<xsl:template match="name">
  Name: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
 </xsl:template>

<xsl:template match="price">
  Price: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    除了其他答案,你的 xml-stylesheet 处理指令

    <?xml-stylesheet type="text/xsl" href="D:\menu.xsl" ?>
    

    错了。首先,href 值应该是一个 URI,而不是 Windows 文件名。其次,根据所选的安全设置,许多浏览器不会从本地文件存储中读取样式表:它们需要 HTTP 连接。

    【讨论】:

    • 这是给学校的,所以现在没关系,但很高兴知道这一点。我很欣赏这种洞察力
    【解决方案2】:

    如果您的输入 XML 文件使用默认命名空间,您必须在样式表中声明它,如下所示:

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:pm="http://www.wasabi.com/pakistan/menu">
    

    然后使用命名空间前缀“pm”来@match 属性或@select 属性:

    <xsl:template match="pm:menu">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="pm:menuItem">
        <p>
            <xsl:apply-templates select="pm:name"/>  
            <xsl:apply-templates select="pm:price"/>
        </p>
    </xsl:template>
    
    <xsl:template match="pm:name">
        Name: <span style="color:#ff0000">
            <xsl:value-of select="."/></span>
        <br />
    </xsl:template>
    
    <xsl:template match="pm:price">
        Price: <span style="color:#ff0000">
            <xsl:value-of select="."/></span>
        <br />
    </xsl:template>
    

    样本结果:

    请注意,您的输入 XML 和样式表文件格式不正确。

    【讨论】:

    • 我在发布后实际上设法纠正了我的错误。不过还是谢谢你花时间帮助我。
    【解决方案3】:

    如上所述,您的 xml 和样式表中存在一些致命错误,导致无法处理它们。一个是省略了 xml 中的关闭 &lt;/menu&gt; 标记,另一个是样式表中未关闭的 &lt;xsl:template ... &gt; 元素。这可能是样式表没有像您报告的那样产生任何输出的原因。

    纠正这些错误后,我们注意到三个模板:menuItem, name,price 根本没有被调用。这确实是因为这些元素具有命名空间:

    xmlns="http://www.wasabi.com/pakistan/menu"
    

    在 XML 输入文件中,但在 xslt 文件中没有命名空间。

    因此,这些模板中的所有 HTML 格式都被忽略了,样式表只是简单地输出未格式化的文本。它输出任何内容的唯一原因是顶级模板 &lt;xsl:template match="/" 匹配 any 命名空间中的 any 文档元素。然后 xslt 的默认级联接管以产生输出,这就是为什么在更正输入文件中的格式正确错误后生成的 html 看起来像这样的原因:

    screenshot 1

    解决此问题的一种方法是按照上述建议在 xml 中提供命名空间前缀,然后您可以在样式表中引用完全限定名称。例如,如果您在 xml 中使用命名空间前缀 pm,那么 xml 中的 all 元素必须以 pm: 为前缀,并且 xml 中的命名空间声明将变为:

    xmlns:pm="http://www.wasabi.com/pakistan/menu"
    

    另一种方法是删除默认命名空间声明

    xmlns="http://www.wasabi.com/pakistan/menu"
    

    来自 xml。我在你的 xml 上试过这个,确实所有的模板都被调用了,html 在浏览器窗口中看起来像预期的那样:

    screenshot 2

    希望这可以帮助您了解这里发生了什么...

    J.

    【讨论】:

    • 我在发布后实际上设法纠正了我的错误。不过还是谢谢你花时间帮助我。
    猜你喜欢
    • 2011-10-17
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多