XSLT根本不显示

时间:2017-11-26 22:15:44

标签: xml xslt

我是使用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>

2 个答案:

答案 0 :(得分:0)

如果您的输入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>

样本结果:

Sample result in Chrome

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

答案 1 :(得分:0)

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

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

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

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

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

screenshot 1

解决此问题的一种方法是通过在xml中提供名称空间前缀,如上所述,然后您可以在样式表中引用完全限定名称。例如,如果在xml中使用了pm的命名空间前缀,则xml中所有元素必须以pm:为前缀,并且命名空间声明在xml将成为:

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

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

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

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

screenshot 2

希望这可以帮助您了解这里发生的事情......

学家

相关问题